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?
2 Answers
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.

- 37,782
- 12
- 108
- 140

- 827
- 8
- 16
-
The problem is that the JEE 5 injection only works with EJBs and not with plain java classes. Regarding the startup point. Yes, I think it would be possible to use the ServletContextListener but since I am not having a web app...create a Servlet just for that? However, I found a solution at least for WebSphere, using StartupBeans and Interceptors. But still, I think I have to use a "Singleton" to get access to the Injector... – Leikingo Sep 09 '11 at 10:04
-
adding a servlet won't be that much work really, especially since you're on JEE 5. If you think you're better off with StartupBeans, go ahead, but generally I try to dissuade people from using proprietary extensions. When some day you end up needing to support JBoss, etc. You will need to redesign. If possible, use WAS8, it supports JEE6/EJB3.1, which defines a singleton bean. – Renan Sep 10 '11 at 02:43
-
+ on the interceptors use, they are a game changer. so sad it took the EJB spec so long to include them. – Renan Sep 10 '11 at 02:44
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

- 603
- 6
- 7
-
Hi and thanks for your suggestion. Unfortunately I'm using Guice for DI. But I could you the Interceptor as well. I'll try that... The problem here, where and how to start the "single" AppContext/Injector and how to access it in the interceptor? By a singleton?! – Leikingo Jul 14 '11 at 14:39
-
@Ingo I haven't used Guice myself but here are posts describing the use of a (session) bean to be the Guice injector 'holder': [http://attias.myftp.org/attias/index.php/Guice_and_EJB], [http://musingsofaprogrammingaddict.blogspot.com/2009/03/guice-tutorial-part-3-integrating-guice.html], [http://a-developer-life.blogspot.com/2010/11/injecting-into-ejb3-with-google-guice.html] – francisnovilla Jul 19 '11 at 10:27