Where should I declare my CDI resources?

Posted by Laird Nelson on Stack Overflow See other posts from Stack Overflow or by Laird Nelson
Published on 2010-06-11T17:23:14Z Indexed on 2010/06/11 17:52 UTC
Read the original article Hit count: 283

Filed under:

JSR-299 (CDI) introduces the (unfortunately named) concept of a resource: http://docs.jboss.org/weld/reference/1.0.0/en-US/html/resources.html#d0e4373

You can think of a resource in this nomenclature as a bridge between the Java EE 6 brand of dependency injection (@EJB, @Resource, @PersistenceContext and the like) and CDI's brand of dependency injection.

The general gist seems to be that somewhere (and this will be the root of my question) you declare what amounts to a bridge class: it contains fields annotated both with Java EE's @EJB or @PersistenceContext or @Resource annotations and with CDI's @Produces annotations. The net effect is that Java EE 6 injects a persistence context, say, where it's called for, and CDI recognizes that injected PersistenceContext as a source for future injections down the line (handled by @Inject).

My question is: what is the community's consensus--or is there one--on:

  • what this bridge class should be named
  • where this bridge class should live
  • whether it's best to localize all this stuff into one class or make several of them


Left to my own devices, I was thinking of declaring a single class called CDIResources and using that as the One True Place to link Java EE's DI with CDI's DI. Many examples do something similar, but I'm not clear on whether they're "just" examples or whether that's a good way to do it.


© Stack Overflow or respective owner

Related posts about cdi

Related posts about weld