I am making a new Java webapp with Spring MVC 3.0 and want to use as much standard Java EE 6 stuff as I can. (I'm on Glassfish 3.1.1.) The real driver is wanting to use an MVC web framework rather than JSF.
So I'm looking for the best way to inject EJBs into my Spring controllers. I had some success but I'm not happy with how it looks and I was hoping to find a better way.
This worked, by finding the EJB via JNDI:
// EJB
@Stateless
public class Service {
@PersistenceContext(name="MAIN")
private EntityManager em;
public void doSomething() { .... }
}
// Spring
@Controller
public class HomeController {
@EJB(mappedName="java:global/springtest/Service")
private Service service;
// controller methods use service
}
But I'm unhappy with needing the "mappedName" on the @EJB annotation in the controller.
Is there a better way to do this?
The good news, though, is that I can use the same @Inject annotation in EJBs and Spring beans and the only difference is which framework is creating the object and doing the injection.