views:

535

answers:

2

Is it possible to inject ejb's into pojo's using the @EJB annotation? If it is, do I have to set up anything special on a JBoss server to make it work?

Please let us not discuss the rationale behind doing this - I am just trying to hack some old code to make it work :-)

+1  A: 

Yes, if you can make JBoss create the Pojo for you.

Your problem is that you probably call new to create the Pojo and the framework can't intercept this which is why @EJB is ignored.

The solution is to use a factory to create your Pojos and tell the factory about the EJBs, so it can set them before returning the new Pojo.

Aaron Digulla
You mean a regular factory pattern created as a bean, right? Factories are not a specific part of the JBoss?!
homaxto
I refer to the Gang of Four pattern: http://en.wikipedia.org/wiki/Factory_pattern
Aaron Digulla
If I use the factory to set the EJB's then I don't use the @EJB, or do I misunderstand something?
homaxto
@Aaron Digulla: Any chance you could provide an example of this?
Tom Tresansky
@Tom Tresansky: My answer should be pretty obvious. If you have a specific question, please post a new question. If you don't understand my answer, then please read up on dependency injection and how @EJB is implemented (you should find an explanation in the docs of your app server or ask the support).
Aaron Digulla
A: 

Not sure, but EJB 3.1 should allow this.

Ondra Žižka