0

I am trying to inject a bean into my main class of the program. I have two modules under Apache Maven:

- Java Parent
  - Module1
  - Module2

Part of the pom file looks like this:

<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>com.screening</groupId>
<artifactId>screening</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Java Parent</name>

<properties>
  <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<modules>
  <module>Module1</module>
  <module>Module2</module>
</modules>
</project>

Module1 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">

 <parent>
    <groupId>com.screening</groupId>
    <artifactId>screening</artifactId>
    <version>1.0-SNAPSHOT</version>
 </parent>

 <modelVersion>4.0.0</modelVersion>

<groupId>com.screening</groupId>
<artifactId>module1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>ejb</packaging>

<name>Module1</name>
</project>

I have a stateless and local bean in Module1:

@Stateless
@Local
public class JDBCProcessorBean implements JDBCProcessorBean {
  // implemented methods
}

I inject this class in the same module:

@Startup
@Singleton
public class Runner {

     @EJB
     private JDBCProcessor jdbcProcessor; // this is injected at runtime
}

The JDBCProcessor is injected at runtime and I can use the methods of JDBCProcessor. The issue comes in when I inject module2 bean into the Runner class. It cannot find the class that I have. Again it has @Stateless and @Local. Even if I try to use @Remote it still cannot find it. I get

 javax.NamingNotFoundException

The only issue is injecting the bean that is in a different module. How can this be fixed?

user3189663
  • 211
  • 4
  • 18

1 Answers1

0

Did you remember to add a beans.xml to src/main/resources/META-INF in the other module?

--UPDATE--

It doesn't actually have to contain any beans. Should pick up an empty one:

<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_0.xsd">
</beans>
jgitter
  • 3,396
  • 1
  • 19
  • 26
  • No. Should this file be blank? – user3189663 Apr 17 '14 at 14:21
  • The formatting in my answer is a little wonky, but that is all you need. This will inform the container that the module contains beans. – jgitter Apr 17 '14 at 14:24
  • Should I be adding this beans.xml to module2? Should I add it to src/main/resources and Apache Maven will add it to META-INF? – user3189663 Apr 17 '14 at 14:25
  • You said this was a maven project. If you put a META-INF folder into your src/main/resources, maven will build it into the jar for you. And yes, it should go in the project that provides the bean. – jgitter Apr 17 '14 at 14:26
  • Here is another related answer if it helps: http://stackoverflow.com/questions/13056336/cdi-beans-xml-where-do-i-put-you – jgitter Apr 17 '14 at 14:27
  • This doesn't work. For unit testing I am using Embedded EJBContainer. Maybe the issue is with the Embedded EJBContainer. If it is, how can I run unit tests in a actual container through Apache Maven? – user3189663 Apr 17 '14 at 14:33
  • I am using NetBeans as the IDE. – user3189663 Apr 17 '14 at 14:41
  • I see. The quick answer is... I wouldn't. You can run integration tests through a container, but unit tests should not. Sorry if I missed that from your questions above. I don't have a lot of experience using EJB in an embedded container, so I probably won't be of much help. I usually mock any beans needed for unit testing. – jgitter Apr 17 '14 at 14:43
  • The reason why it is failing is because Apache Maven is not putting the beans.xml into the classpath. So when module1 is run module2 classes beans.xml is in META-INF folder but cannot be seen in module1. How can I fix this? I believe this is the issue. – user3189663 Apr 17 '14 at 14:53