1

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

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

在 EJB 项目中,我添加了env-entry一个ejb-jar.xml

<?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") private String configFile;

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

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

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

谢谢!

编辑:

将会话元素修改为

<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>
4

2 回答 2

1

我通过修改ejb项目的依赖项来解决这个问题,然后将和项目包装pom.xml到一个项目中。Web 存档的现在看起来像这样:providedwarejbearpom.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>

@Resource当我从项目部署到服务器时,注入现在正在进行env-entriesejbejb-jar.xmlear

于 2013-06-13T19:43:12.380 回答
0

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

于 2013-04-19T06:44:53.777 回答