WebSphere <env-entry>在application.xml和@Resource injection中。

问题描述 投票:0回答:1

我不能将环境条目从 应用程序.xml 使用 @Resource 注解的WebSphere 8.5.5应用程序中。(我不想把入口移动到组件级别) ejb-jar.xml.) 我缺少什么?

Application.xml。

<application
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd"
        version="6">

    <env-entry>
        <env-entry-name>fileName</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>theValue</env-entry-value>
    </env-entry>

有了这个,我 请参阅WebSphere Admin Console部分中定义为 "theValue "的 "fileName"。应用程序的环境条目. 但我却不知道如何从我的EJB中访问它。

StartupBean.java:

@Singleton
@Startup
public class StartupBean {
    @Resource(name = "fileName")
    private String fileName = "default";

fileName 总是 "default",而不是 "theValue"。

我试着用 "java:comp "和 "java:app "进行手动查找(正如我的建议一样 这条),但都扔 NamingException

Context ctx = new InitialContext();
fileName = (String) ctx.lookup("java:comp/env/fileName");
fileName = (String) ctx.lookup("java:app/env/fileName");
java websphere jndi java-ee-6
1个回答
1
投票

资源声明在 application.xml 必须使用 java:app/envjava:global/env 前缀,所以它应该是这样的。

<env-entry>
    <env-entry-name>java:app/env/fileName</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>theValue</env-entry-value>
</env-entry>

然后,注入该领域将如下:

@Resource(name = "java:app/env/fileName")
private String fileName = "default";
© www.soinside.com 2019 - 2024. All rights reserved.