How to use @Resource annotation?

C

Chen

I have a piece of java code here.



@PersistenceUnit
private EntityManagerFactory emf;

@Resource
private UserTransaction utx;


I can find persistence unit in persistence.xml configuration file. But
I cannot find Resource configuration information in a descriptor file.
How does the container do resource injection? Thank you very much.

Chen
 
O

Owen Jacobson

I have a piece of java code here.

    @PersistenceUnit
    private EntityManagerFactory emf;

    @Resource
    private UserTransaction utx;

I can find persistence unit in persistence.xml configuration file. But
I cannot find Resource configuration information in a descriptor file.
How does the container do resource injection? Thank you very much.

In general, the container looks at both the class and the name of an
injectable property. For certain types (like UserTransaction and
EJBContext), if there is a single property with that type, then the
appropriate object is injected regardless of name; for any other type,
and for "standard" types with multiple properties, the container looks
for a JNDI entry in the component's context (java:comp/env/).

If the annotation carries a name=, the name is used to identify the
resource in JNDI to inject: @Resource (name="foo") will inject
java:comp/env/foo to the appropriate field. Otherwise, a default name
is used; I believe it's the class and property name, with dots:
java:comp/env/com.example.MyStatelessBean.someProperty . The third
option is to use the @Resource (mappedName="bar") mapped name
parameter; the interpretation of mapped names is entirely up to the
container (but they're usually global JNDI names rather than component-
relative JNDI names).

-o
 
C

Chen

Thank you for your answer.

But where do I find the configuration information of UserTransaction?
Is there a description file? Thanks.
 
O

ojacobson

Thank you for your answer.

But where do I find the configuration information of UserTransaction?
Is there a description file? Thanks.

UserTransaction instances are part of the container's JTA
implementation. If you really want the gory details, that's the spec
to read. Any configuration for them is vendor-specific; JTA only
mandates an API, and that a UserTransaction instance be available to
managed components (EJBs, servlets, etc) via JNDI.

-o
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top