EJB/MDB 应用程序中的 DI
我目前正在开发一个在 IBM Websphere Application Server 7 (Java EE 5) 上运行的小型 EJB 应用程序。该应用程序主要由一个 MDB 组成,该 MDB 监听传入的 MQ 消息,这些消息被转换并存储在数据库中。目前,我使用大量单例/工厂来共享配置、映射、数据源查找等。但这实际上会导致一些非常难以测试的代码。解决方案可能是使用(简单的)DI 框架(如 guice/spring)来注入不同的实例。问题是:初始化/设置代码放在哪里?应用程序的主要入口点在哪里?如何将实例注入 MDB?
I'm currently developing a small EJB application running on IBM Websphere Application Server 7 (Java EE 5). The app mainly consists of one MDB listening for incoming MQ messages which are transformed and stored in a DB. Currently I'm using a lot of Singleton/Factories to share configurations, mappings, datasource lookups etc. But this actually leads to some very hard to test code. The solution might be using a (simple) DI framework like guice/spring to inject the different instances. The question is: Where to place the initialization/ setup code? Where is the main entry point of the application? How can I inject instances into the MDBs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能值得考虑放弃使用 Guice,并尝试使用 Java EE 5 中已有的注入机制。
关于寻找合适的“启动点”,不幸的是,EJB 规范没有定义一种方法,您可以在其中使用bean 在启动时运行。然而,EE 规范的 Web 配置文件确实有一个 —— 您可以向应用程序添加 WAR,并设置 servlet 侦听器组件:
http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContextListener.html
你可以设置这个每当容器 (WebSphere) 加载并启动应用程序时启动。不过要注意类加载器问题。
it might be worth looking at backing off from using Guice, and trying to work with the injection mechanisms already available with Java EE 5.
Regarding finding a suitable "startup point", unfortunately the EJB specification does not define a way where you can have a bean run at startup. However, the web profile of the EE spec does have one -- you can add a WAR to your application, and set a servlet listener component:
http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContextListener.html
You can set this to start whenever the application is loaded and started by the container (WebSphere). Watch out for classloader issues though.
使用 Spring,您可以通过 EJB3 拦截器来完成此操作,请参阅 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/ejb.html#ejb-implementation-ejb3
有用信息关于注意事项在javadoc中,请确保您阅读它: http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/ejb/interceptor/SpringBeanAutowiringInterceptor.html
Using Spring, you can do it via EJB3 interceptors, see http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/ejb.html#ejb-implementation-ejb3
Useful info on caveats are in the javadoc, make sure you read it: http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/ejb/interceptor/SpringBeanAutowiringInterceptor.html