Is there a Spring annotation that replaces ClassPa thanksmlApplicationContext.getBean?

I defined the MyFrontService class in the bank, this is how I want to use it:

import blabla.MyFrontService; public class Main { public static void main(String[] args) { MyFrontService.doThis(); } } 

FrontService serves as an entry point for accessing other services. This is how it is currently defined (and it works).

MyFrontService.java:

 public class MyFrontService { public static void doThis(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/springBeans.xml"); MyService1 myService1 = (MyService1) context.getBean("myService1"); myService1.doSomething(); context.close(); } } 

MyService1.java:

 package blabla.service; @Service("myService1") public class MyService1 { public void doSomething(){ // some code } } 

SRC / main / resources / META-INF / springBeans.xml:

 (...) <context:annotation-config /> <context:component-scan base-package="blabla.service" /> (...) 

I would like to replace the MyFrontService.java code with something like this:

 @MagicAnnotation("what_path?/springBeans.xml") public class MyFrontService { @Autowired private static MyService1 myService1; public static void doThis(){ myService1.doSomething(); } } 

I read a lot from other questions on this site and others. Sometimes they say that this is impossible, and sometimes annotations such as @Configuration, @Import and others are used, but I can not get them to work. For example, using

 @ImportResource("classpath:META-INF/springBeans.xml") 

throws a NullPointerException when calling myService1.doSomething ().

If possible, which annotation and which way should I use?

+5
source share
2 answers

Use Spring Download and you can write smth like this

 @Controller @EnableAutoConfiguration public class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } } 
+1
source

if you mark your MyFrontService class with @Component annotation, which should save you a headache when doing everything you do. if you do not want to add annotation and do not want to have everything in spring config, you can define a class as shown below.

 import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; public static ApplicationContext getApplicationContext() { return applicationContext; } @Override public void setApplicationContext( org.springframework.context.ApplicationContext ctx) throws BeansException { applicationContext = ctx; } } 

And then in any class you can do

 ApplicationContext ctx = SpringContextUtil.getApplicationContext(); BeanClass av = ctx.getBean(BeanClass.class); 
-1
source

Source: https://habr.com/ru/post/1246579/


All Articles