1

我正在尝试使用 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 未注入。

一些想法?

提前致谢。

扫罗

4

1 回答 1

0

您不需要指定全局 jndi 名称。像这样在你的 servlet 中注入它:

@EJB
private PruebaBL pruebaBL;
于 2013-02-07T15:04:20.713 回答