如何从EJB中访问部署参数 [英] How to access deployment parameter from within EJB

查看:211
本文介绍了如何从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 描述符中定义一个Context参数,我可以通过 javax.faces.context.ExternalContext#getInitParameterMap(),当我在一个 JSF bean中,通过 Servlet 中的servlet / GenericServlet.html#getServletContext%28%29rel =nofollow> getServletContext(),但情况并非如此,确实是因为我在一个 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?

推荐答案

经过进一步研究,我发现使用 env-entry a在 web.xml 中的nnotation:

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>        

可以访问 env-entry EJB 的各种方式。最简单的是使用 @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;

链接:使用ENC将envirnoment条目配置为EJB 3.

Links: Configure your EJB 3 with envirnoment entries using ENC

这篇关于如何从EJB中访问部署参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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