【发布时间】:2013-01-12 20:17:27
【问题描述】:
我正在尝试使用 EJB3 注释注入 PersistenceContext,但 geronimo 不注入依赖项。这是一个由EJB和WEB模块组成的EAR项目。
EJB 模块配置
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ejb:openejb-jar
xmlns:app="http://geronimo.apache.org/xml/ns/j2ee/application-2.0"
xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:client="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0"
xmlns:conn="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2"
xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2" xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2"
xmlns:jaspi="http://geronimo.apache.org/xml/ns/geronimo-jaspi"
xmlns:log="http://geronimo.apache.org/xml/ns/loginconfig-2.0"
xmlns:name="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pers="http://java.sun.com/xml/ns/persistence"
xmlns:pkgen="http://openejb.apache.org/xml/ns/pkgen-2.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0"
xmlns:web="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1">
<dep:environment>
<dep:moduleId>
<dep:groupId>wedge</dep:groupId>
<dep:artifactId>wedge-ejb</dep:artifactId>
<dep:version>1.0</dep:version>
<dep:type>car</dep:type>
</dep:moduleId>
<dep:dependencies>
<dep:dependency>
<dep:groupId>console.dbpool</dep:groupId>
<dep:artifactId>jdbc_wedgeDS</dep:artifactId>
<dep:version>1.0</dep:version>
<dep:type>car</dep:type>
</dep:dependency>
</dep:dependencies>
</dep:environment>
我已经配置了 persistence.xml 如下
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="wedgePU" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>jdbc/wedgeDS</jta-data-source>
<class>wedge.entity.Aec</class>
...
<class>wedge.entity.Tnr</class>
<properties>
<property name="openjpa.TransactionMode" value="managed" />
<property name="openjpa.ManagedRuntime"
value="jndi(TransactionManagerName=java:comp/TransactionManager)" />
<property name="openjpa.Log" value="DefaultLevel=INFO" />
</properties>
</persistence-unit>
我已将数据源创建为本地 TX 数据源,并且已解决。
我已经定义了一个 EJB 如下
@Local(PruebaBL.class)
@Stateless
public class PruebaBLImpl {
@PersistenceContext()
private EntityManager em;
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void metodoPrueba(HttpServletResponse response) throws IOException, NamingException {
if (em == null) {
response.getOutputStream().println("entity manager is null");
}
我已验证事务开始/结束正常,但未注入 entityManager。
一些想法?
提前致谢。
索尔
【问题讨论】:
-
我正在阅读日志和调试,我认为问题在于我如何尝试将 EJB 注入 WebServlet。我是否需要在 @EJB 注释中指定 jndi 全局名称?即
@EJB (name=java:comp/env/global/wedge/wedge-ejb/PruebaBLImpl!wedge.ejb.PruebaBL)。如果此引用失败,Pojo 将被创建为 'new',并且注入失败。这是正确的?谢谢
标签: dependency-injection ejb-3.0 openjpa entitymanager geronimo