如何从 EJB 中访问部署参数 [英] How to access deployment parameter from within EJB
问题描述
在 Java EE 6 Web 应用程序中,我想从 EJB
中访问部署参数(字符串值).
In a Java EE 6 Web application, I would like to access a deployment parameter (a string value) from within an EJB
.
我知道我可以在 web.xml
描述符中定义一个上下文参数,并且我将能够通过 javax.faces.context.ExternalContext#getInitParameterMap(),当我在 JSF
bean,并通过 getServletContext() 来自 Servlet
,但事实并非如此,因为我在 EJB
中.
I know that I can define a Context Parameter in web.xml
descriptor, and I'll be able to access it through javax.faces.context.ExternalContext#getInitParameterMap(), when I am in a JSF
bean, and through getServletContext() from within a Servlet
, but this is not the case, indeed, because I am in an EJB
.
那么,问题是:是否有任何标准(并且可能是干净的)方法来实现这一目标?
So, the question is: is there any standard (and possibly clean) way to accomplish this goal?
推荐答案
经过深入研究,发现web.xml
中env-entry
注解的使用.:
After further research, I have found out the use of env-entry
annotation in web.xml
.:
<env-entry>
<env-entry-name>myEnvEntry</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>MyEnvEntryValue</env-entry-value>
</env-entry>
可以从 EJB
以各种方式访问 env-entry
.最简单的就是使用@Resource
注解(需要CDI):
The env-entry
can be accessed in various ways from an EJB
. The simplest is the use of @Resource
annotation (requires CDI):
@Resource(name="myEnvEntry")
private String myEnvEntry;
这篇关于如何从 EJB 中访问部署参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!