Question

I am learning JavaEE 7 recently. I have a problem when I try to inject an ejb from a separate jar file.

My code is like

@Stateless
public class HelloService {

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

}

The HelloService is in independed project and I have published it into the glassfish4 server as ejb file.

Then I have JSF project, right now I only have one backing bean

@Model
public class IndexBean {

    @Inject
    private HelloService helloService;

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

After build the JSF project. I published it to the same glassfish4 server. But in my JSF project, it can't inject HelloService bean from other jar file.

I tried to add @Remote on the HelloService class, it's still not inject-able.

I have 3 pom files.

Parent Project:

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

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

<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>
Was it helpful?

Solution

Take a look here: https://stackoverflow.com/a/6969520/3431758

You can't use local access to EJB from other module (javaee7-jsf in your case). If you want to use local access to EJB create EAR project or include EJB in your WAR file (by skipping <scope>provided</scope> in pom.xml of your javaee7-samples module).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top