views:

146

answers:

1

Hello, I want to be able to do something like this in a stateless Session Bean

@Resource(name="mycustomthingie") private CustomClass stuff;

The value injected is context (speak: Thread) dependant. I guess this would be possible if I bind an ObjectFactory into the JNDI Context that delivers the correct Object when requested.

As I understand it for this to work I would have to add the resource to the "java:comp" JNDI context, but this is appearantly not allowed by the EE5 specification. Is there a subcontext in which I am allowed to write or is there another way to get something similar to work ?

Thanks

Peter

+1  A: 

According to this article from InfoQ that does a good job at summarizing the Resource Injection part of the JSR-244 (I couldn't find this in one place in the spec):

Injection is limited only to first class constructs defined in the Java EE platform, including:

  • SessionContext object
  • DataSources object
  • UserTransaction
  • EntityManager interface
  • TimerService interface
  • Other enterprise beans
  • Web services
  • Message queues and topics
  • Connection factories for resource adaptes
  • Environment entries limited to String, Character, Byte, Short, Integer, Long, Boolean, Double, and Float.

The injection facilities in Java EE 5 do not apply to any POJO (which is often criticized by the Spring community) [...]

So if your CustomClass is not a managed component and not a simple environment entry (which doesn't seem to be the case), I don't think you can inject it.

Pascal Thivent
The environemnt entries brought me to this Idea on this page: http://java.sun.com/developer/technicalArticles/J2EE/injection/I was hoping something could be done with a resource ref or something like that.
ptriller