【问题标题】:PersitenceContext injection in EJB module in Geronimo 3.0Geronimo 3.0 中 EJB 模块中的 PersitenceContext 注入
【发布时间】: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


【解决方案1】:

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

@EJB
private PruebaBL pruebaBL;

【讨论】:

  • 我以这种方式成功部署了 EJB,即使没有 @Local。问题仍然存在,我无法使用在 EJB 和 Servlet 中注入的 persistenceContext。这个周末我将使用 postgresql 进行相同的配置,部署不同的 XA 数据源。谢谢
猜你喜欢
  • 2012-09-11
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 2016-09-26
  • 2012-08-23
  • 1970-01-01
  • 2013-12-29
  • 2017-03-13
相关资源
最近更新 更多