I am currently working on a Spring Boot project, consisting of a full-stack web application, using Spring JPA for the persistence, services for the businness level and Spring MVC for the REST endpoints.
So far so good, but I've been required to add functionalities implemented in pure Java EE. I immediately thought about "inserting" a couple of plain old Enterprise Java Beans in some way into the current Spring Boot application.
So far I've tried different possibilites, but nothing worked. The EJb is something like
@Stateless(name = "TeacherService")
public class TeacherServiceImpl implements TeacherService {
....
}
I'd like to inject it into a Spring RestController like this:
@RestController
public class ExampleController {
@EJB / @Inject / @Autowire
private TeacherService teacherService;
@GetMapping(value = "user")
public String getSomeData() {
return teacherService.someMethod();
}
}
I tried several combinations but nothing worked so far. I found some info around, but it looks like the only way to merge EJB with Spring is to use the complete Spring Framework instead of Spring Boot. Before anyone points that out, I know it's a stupid task and I could simply declare the class as a Spring Bean with something like @Service, or @Component, but the assignment states to "provide some functions using plain Java EE" and those annotations belong to Spring.
Is there any workaround (or anything I haven't tried) for this kind of task? Alternatively, what component of Java EE could be easily compatible with Spring Boot? Thank you for your time.
Spring supports the JSR-330 (@Inject and @Named).
Unfortunately, the @Stateless is not supported.
But the way Spring can solve your problem, is just by declaring the TeacherServiceImpl as a bean in a Configuration class.
@Configuration
public class MyConfig {
@Bean
public TeacherService teacherService() {
return new TeacherServiceImpl();
}
}
You will then be able to @Autowire/@Inject the bean.
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