I have defined a class MyFrontService in a jar, here is how I want to use it:
import blabla.MyFrontService;
public class Main {
public static void main(String[] args) {
MyFrontService.doThis();
}
}
The FrontService serves as an entry point to access other services. Here 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 code of MyFrontService.java by something like this:
@MagicAnnotation("what_path?/springBeans.xml")
public class MyFrontService {
@Autowired
private static MyService1 myService1;
public static void doThis(){
myService1.doSomething();
}
}
I've read a lot from other questions on this website and others. It is sometimes said that it's impossible, and sometimes annotations like @Configuration, @Import and others are used but I can't make them work. For example, using
@ImportResource("classpath:META-INF/springBeans.xml")
triggers a NullPointerException when calling myService1.doSomething().
If it is possible to do it, what annotation and what path should I use?
Use Spring Boot framework and you will be able to 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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With