使用Spring MVC 3.0和@EJB注释而不使用mappedName?
我正在使用 Spring MVC 3.0 制作一个新的 Java Web 应用程序,并且希望尽可能多地使用标准 Java EE 6 的东西。 (我使用的是 Glassfish 3.1.1。)真正的驱动程序是想要使用 MVC Web 框架而不是 JSF。
因此,我正在寻找将 EJB 注入到我的 Spring 控制器中的最佳方法。我取得了一些成功,但我对它的外观不满意,我希望找到更好的方法。
通过 JNDI 查找 EJB,这很有效:
// 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
}
但我对控制器中的 @EJB 注释需要“mappedName”感到不满意。
有更好的方法吗?
不过,好消息是我可以在 EJB 和 Spring bean 中使用相同的 @Inject 注释,唯一的区别是哪个框架正在创建对象并执行注入。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用
代替,
则不必担心应用程序名称。
这使得代码更加可移植。我想这会解决你的一些问题
If you use
instead of
you do not have to worry about the appname.
This makes the code more portable. I guess that will solve some of your problems
对于 Spring,您可以将 JNDI 查找包装到
JndiObjectFactoryBean
中:然后您可能(如果我错了请纠正我)能够将它与
@ 一起使用注入
不使用@Named("serviceBean")
注释:For Spring, you could wrap the JNDI lookup into
JndiObjectFactoryBean
:Then you'll probably (correct me if I'm wrong) be able to use it with
@Inject
without the@Named("serviceBean")
annotation: