如何在编译时指定资源注释的名称? [英] How to specify name for resource annotations in compile time?

查看:91
本文介绍了如何在编译时指定资源注释的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的代码有这样的:

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

但是,在一个部署方案中,队列注释应如下所示:

However, in one deployment scenario the queue annotation should look like this:

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

我想选择与Maven构建配置文件一起使用的名称。

I would like to choose the name to use with Maven build profiles.

我有哪些选择?

推荐答案

这不是正确的做事方式。应将资源添加到各个EJB的本地jndi名称中。这是为了将bean代码中使用的jndi名称与bean部署者设置的全局jndi绑定分开。 bean 本地jndi绑定全局绑定的映射可以通过 ejb-jar.xml 来处理,特定于appserver的部署描述符。

This is not the right way to do things. Resources should be added to the local jndi name of individual EJBs. This is to separate the jndi name used in the bean code from the global jndi bindings set by the bean deployer. The mapping of the bean local jndi binding and the global binding may be handled via the ejb-jar.xml and appserver-specific deployment descriptors.

所以,你应该声明你的 @Resource (相当于一个< resource-ref> 元素,表示资源引用名称和类型),如下所示:

So, instead, you should declare your @Resource (which is equivalent to a <resource-ref> element indicating resource reference name and type) like this:

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

然后,在特定于appserver的部署描述符中(对于GlassFish,它是 sun- ejb-jar.xml ,对于JBoss,它是 jboss.xml ,对于WebLogic,它的 weblogic-ejb-jar.xml 等),声明一个< resource-ref> 元素,通过指示资源引用名称和全局jndi绑定< jndi-name> 元素。

Then, in a appserver-specific deployment descriptor (for GlassFish it's sun-ejb-jar.xml, for JBoss it's jboss.xml, for WebLogic it's weblogic-ejb-jar.xml, etc), declare a <resource-ref> element indicating the resource reference name and the global jndi binding via the <jndi-name> element.

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

一旦你完成所有工作,就可以很容易地改变这个特定于appserver的部署描述符使用Maven用于具有配置文件和过滤的不同环境。只需使用属性,激活资源过滤,并在配置文件中设置不同的值。类似的东西:

Once you'll get the whole thing working, it will be easy to variabalize this appserver-specific deployment descriptor using Maven for different environments with profiles and filtering. Just use a property, activate filtering of resources, and set different value in profiles. Something like that:

<resource-ref>
  <res-ref-name>jms/queue/aQueue</res-ref-name>
  <jndi-name>${my.jndi.name}</jndi-name>
</resource-ref>

这篇关于如何在编译时指定资源注释的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆