我正在尝试将bean注入我的程序主类。我在Apache Maven下有两个模块:
- Java Parent
- Module1
- Module2
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>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>
我在Module1中有一个无状态和本地bean:
@Stateless
@Local
public class JDBCProcessorBean implements JDBCProcessorBean {
// implemented methods
}
我在同一个模块中注入了这个类:
@Startup
@Singleton
public class Runner {
@EJB
private JDBCProcessor jdbcProcessor; // this is injected at runtime
}
JDBCProcessor在运行时注入,我可以使用JDBCProcessor的方法。当我将module2 bean注入Runner类时会出现问题。它找不到我所拥有的课程。它还有@Stateless和@Local。即使我尝试使用@Remote它仍然无法找到它。我得到了
javax.NamingNotFoundException
唯一的问题是注入不同模块中的bean。如何解决这个问题?
答案 0 :(得分:0)
你还记得在其他模块中将bean.xml添加到src / main / resources / META-INF吗?
- UPDATE -
它实际上不必包含任何bean。应该拿一个空的:
<?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>