Java – how do I specify the name of a resource annotation at compile time?

Our code has this:

@Resource(name = "java:comp/resource/foo/bar/ONE_QUEUE")
private Queue queue;

However, in a deployment scenario, the queue comments should be as follows:

@Resource(name = "java:comp/resource/foo/bar/SECOND_QUEUE")
private Queue queue;

I want to choose a name to use with the Maven build configuration file

What choice do I have?

Solution

This is not the right way to do things Resources should be added to the local JNDI name of each EJB This is to separate the JNDI name used in the bean code from the global JNDI binding set by the bean deployer You can use EJB jar XML and app server specific deployment descriptors to handle the mapping of bean local JNDI binding and global binding

Therefore, you should declare your @ resource (equivalent to the < resource ref > element indicating the resource reference name and type), as follows:

@Resource(name = "jms/queue/aQueue")
private Queue queue;

Then, in the deployment descriptor specific to the app server (for GlassFish, it is sun-ejb-jar.xml, for JBoss, it is jboss.xml, for Weblogic, it is weblogic-ejb-jar.xml, etc.), declare a < resource ref > element to represent the resource reference name and global JNDI binding element through < JNDI name >

<resource-ref>
  <res-ref-name>jms/queue/aQueue</res-ref-name>
  <jndi-name>resource/foo/bar/ONE_QUEUE</jndi-name>
</resource-ref>

Once you have finished the whole work, you can easily use Maven to deform the app server specific deployment descriptor to adapt to different environments with configuration files and filtering Just use properties, activate resource filtering, and set different values in the configuration file Something like this:

<resource-ref>
  <res-ref-name>jms/queue/aQueue</res-ref-name>
  <jndi-name>${my.jndi.name}</jndi-name>
</resource-ref>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>