【问题标题】:env-entry in ejb-jar.xml not being injected with @Resource when deployed inside WAR file在 WAR 文件中部署时,ejb-jar.xml 中的 env-entry 未注入 @Resource
【发布时间】:2013-04-10 21:17:09
【问题描述】:

我有一个依赖于 EJB 项目的 Maven Web 应用程序。

   <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>soar-ejb</artifactId>
        <version>1.0</version>
        <type>jar</type>
    </dependency>

在 EJB 项目中,我在 ejb-jar.xml 中添加了一个 env-entry,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee" 
     version = "3.1"
         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/ejb-jar_3_1.xsd">
    <enterprise-beans>
        <session>
            <env-entry>
                <description>Config file</description>
                <env-entry-name>configFileLocation</env-entry-name>
                <env-entry-type>java.lang.String</env-entry-type>
                <env-entry-value>dropbox-config.properties</env-entry-value>
            </env-entry>
        </session>
    </enterprise-beans>
</ejb-jar>

我已经使用 arquillian 测试了 EJB 项目,我可以使用 @Resource 注入这个值 因此: @Resource(name = "configFileLocation") 私有字符串配置文件;

现在,当我使用 ejb 依赖项构建 .war 时,我会在 WEB-INF\lib 中获得一个包含我的 EJB 项目的 .war 作为 .jar 文件。在这个 EJB 项目中(即 .jar 中),ejb-jar.xml 文件位于正确的 META-INF 目录中。

但是现在,当我部署到服务器时,@Resource 注入永远不会起作用。 String 始终为 null。根据我所读到的,我在 EJB 项目和 .war 生成的 .war 中都有正确的位置 ejb-jar.xml

有人知道我的配置不正确吗?

谢谢!

编辑:

将会话元素修改为

<session>
        <description>An EJB that loads configuration from a file</description>
        <display-name>ConfigurationProducer</display-name>
        <ejb-name>ConfigurationProducer</ejb-name>
        <ejb-class>com.trf.util.DropboxConfigFileProducer</ejb-class>
        <session-type>Stateless</session-type>
        <env-entry>
            <description>Location of the config file</description>
            <env-entry-name>configFileLocation</env-entry-name>
            <env-entry-type>java.lang.String</env-entry-type>
            <env-entry-value>dropbox-config.properties</env-entry-value>
        </env-entry>
    </session>

【问题讨论】:

    标签: jakarta-ee maven-2 glassfish-3 ejb-3.1 ejb-jar.xml


    【解决方案1】:

    我通过将pom.xml 中的ejb 项目的依赖关系修改为provided,然后将warejb 项目包装到ear 项目中来解决了这个问题。网络档案的pom.xml 现在看起来像这样:

        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>soar-ejb</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>
    

    然后在ear 项目的pom.xml 我们有这个:

     <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>soar-ejb</artifactId>
            <version>1.0</version>
            <type>ejb</type>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>soar-war</artifactId>
            <version>1.0</version>
            <type>war</type>
        </dependency>
    </dependencies>
    

    当我从ear 项目部署到服务器时,@Resource 注入现在正在处理ejbejb-jar.xml 中的env-entries

    【讨论】:

      【解决方案2】:

      您的 ejb-jar.xmlsession 元素不包含 ejb-name 属性,请尝试使用name 匹配 bean 接口名称。我已经编辑了你的答案,向你展示了一个例子。

      【讨论】:

      • Remigo,我已将 ejb-name 元素添加到 session 元素中,但资源仍未注入。当我自己测试 ejb 项目时仍然有效。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      相关资源
      最近更新 更多