2014-03-26 39 views
1

我最近在學習JavaEE 7。當我嘗試從單獨的jar文件注入ejb時遇到問題。如何從同一個應用程序中的另一個ejb文件注入EJB?

我的代碼是這樣

@Stateless 
public class HelloService { 

    public String hello(String name) { 
     return "Hello " + name; 
    } 

} 

的HelloService的是independed項目,我把它發佈到glassfish4服務器作爲EJB文件。

然後,我有JSF項目,現在我只有一個支持bean

@Model 
public class IndexBean { 

    @Inject 
    private HelloService helloService; 

    public String getName() { 
     return helloService.hello("World"); 
    } 
} 

構建JSF項目後。我將它發佈到同一個glassfish4服務器。但在我的JSF項目中,它不能從其他jar文件注入HelloService bean。

我試圖在HelloService類中添加@Remote,但它仍然不能注入。

我有3個pom文件。

父項目:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>org.chris.sample</groupId> 
    <artifactId>javaee7-samples</artifactId> 
    <version>1.0.0</version> 
    <packaging>pom</packaging> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-api</artifactId> 
      <version>7.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.derby</groupId> 
      <artifactId>derby</artifactId> 
      <version>10.10.1.1</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.1</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
     </plugins> 

    </build> 
    <modules> 
     <module>javaee7-jsf</module> 
     <module>javaee7-ejb</module> 
    </modules> 
</project> 

JSF項目:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <parent> 
     <groupId>org.chris.sample</groupId> 
     <artifactId>javaee7-samples</artifactId> 
     <version>1.0.0</version> 
    </parent> 
    <artifactId>javaee7-jsf</artifactId> 
    <packaging>war</packaging> 

    <dependencies> 
     <dependency> 
      <groupId>org.chris.sample</groupId> 
      <artifactId>javaee7-ejb</artifactId> 
      <type>jar</type> 
      <version>1.0.0</version> 
      <scope>provided</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.4</version> 
       <configuration> 
        <failOnMissingWebXml>false</failOnMissingWebXml> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

EJB項目:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <parent> 
    <groupId>org.chris.sample</groupId> 
    <artifactId>javaee7-samples</artifactId> 
    <version>1.0.0</version> 
    </parent> 
    <artifactId>javaee7-ejb</artifactId> 
    <packaging>ejb</packaging> 
</project> 

回答

2

到這裏看看:https://stackoverflow.com/a/6969520/3431758

您不能使用本地從其他模塊訪問EJB(在您的情況下爲javaee7-jsf)。如果要使用本地訪問EJB創建EAR項目或在WAR文件中包含EJB(通過在javaee7-samples模塊的pom.xml中跳過<scope>provided</scope>)。

+0

您的回答非常有幫助。謝謝 – Chris

相關問題