2

我在使用焊接记录器注入时遇到了一些问题。

场景:我的耳朵里面有 ejb-jar。

这是我的 ejb bean:

@Stateless
@LocalBean
public class PartnersService {
    @Inject
    Logger log;

    @PersistenceContext(unitName = "Utopia")
    EntityManager em;

    public PartnersService() {
    }

    public OasysPartnerEntity getPartner(long id){
        return em.find(OasysPartnerEntity.class, id);
    }

    @Schedule( hour = "*", minute = "*", second = "*/15")
    public void print1Partner(){
        System.out.println("This is test");
        log.info("This is partner`s email under id 1 = ");
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void saveTempPartnerTemp(OasysPartnerTempEntity part){
        em.persist(part);
    }

}

当 print1Partner 调用我得到异常:

|2010-07-02T19:25:35.003+0300|警告|oracle-glassfish3.0.1|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=48;_ThreadName=Thread-1;|调用 EJB PartnersService 方法时发生系统异常 public void ua.co.oasys.fenix.persistence.PartnersService.print1Partner() javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: 无法创建无状态EJB 在 com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:448) 在 com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2467) 在 com.sun.ejb.containers.BaseContainer。 preInvoke(BaseContainer.java:1860) 在 com.sun.ejb.containers.BaseContainer.callEJBTimeout(BaseContainer.java:3962) 在 com.sun.ejb.containers.EJBTimerService。com.sun.ejb.containers.EJBTimerService.access$100(EJBTimerService.java:98) 上的 deliverTimeout(EJBTimerService.java:1667) com.sun.ejb.containers.EJBTimerService$TaskExpiredWork.run(EJBTimerService.java:2485) java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 在 java.util.concurrent.FutureTask.run(FutureTask. java:138) 在 java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 在 java.lang.Thread.run (Thread.java:619) 原因:javax.ejb.EJBException:javax.ejb.CreateException:无法在 com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory 创建无状态 EJB。在 com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:443) 处创建(StatelessSessionContainer.java:720) ... 12 更多原因:javax.ejb.CreateException: 无法在 com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:528) 在 com.sun.ejb.containers.StatelessSessionContainer.access 创建无状态 EJB $000(StatelessSessionContainer.java:90) at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:718) ... 14 更多原因:java.util.concurrent.ConcurrentHashMap 的 java.lang.NullPointerException .get(ConcurrentHashMap.java:768) 在 org.jboss.weld.manager.BeanManagerImpl。getBean(BeanManagerImpl.java:1171) at org.jboss.weld.manager.BeanManagerImpl.getBean(BeanManagerImpl.java:132) at org.glassfish.weld.services.JCDIServiceImpl._createJCDIInjectionContext(JCDIServiceImpl.java:145) at org.glassfish .weld.services.JCDIServiceImpl.createJCDIInjectionContext(JCDIServiceImpl.java:122) 在 com.sun.ejb.containers.BaseContainer.createEjbInstanceAndContext(BaseContainer.java:1616) 在 com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java :469) ... 还有 16 个 |#]createJCDIInjectionContext(JCDIServiceImpl.java:122) at com.sun.ejb.containers.BaseContainer.createEjbInstanceAndContext(BaseContainer.java:1616) at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:469) ... 16更多 |#]createJCDIInjectionContext(JCDIServiceImpl.java:122) at com.sun.ejb.containers.BaseContainer.createEjbInstanceAndContext(BaseContainer.java:1616) at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:469) ... 16更多 |#]

使用:ejb 3.1,glassfish 3.0.1

一些maven依赖:

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.ejb</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>
<!-- SL4J API -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- SLF4J JDK14 Binding  -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.6.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Injectable Weld-Logger -->
        <dependency>
            <groupId>org.jboss.weld</groupId>
            <artifactId>weld-logger</artifactId>
            <version>1.0.0-CR2</version>
            <scope>provided</scope>
        </dependency>
            <!--CDI-->
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <scope>provided</scope>
            <version>1.0-CR4</version>
        </dependency>

耳朵 pom.xml

<dependencies>
....
<!--weld-->
        <!-- SL4J API -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.0</version>
        </dependency>

        <!-- SLF4J JDK14 Binding  -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.6.0</version>
        </dependency>

        <!-- Injectable Weld-Logger -->
        <dependency>
            <groupId>org.jboss.weld</groupId>
            <artifactId>weld-logger</artifactId>
            <version>1.0.0-CR2</version>
        </dependency>

        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>

            <version>1.0-CR4</version>
        </dependency>


    </dependencies>
<build>
...
<configuration>
                    <modules>
...
                       <!--weld-->
                        <!-- SL4J API -->
                        <jarModule>
                            <groupId>org.slf4j</groupId>
                            <artifactId>slf4j-api</artifactId>
                            <bundleDir>lib</bundleDir>
                        </jarModule>

                        <!-- SLF4J JDK14 Binding  -->
                        <jarModule>
                            <groupId>org.slf4j</groupId>
                            <artifactId>slf4j-jdk14</artifactId>
                            <bundleDir>lib</bundleDir>
                        </jarModule>

                        <!-- Injectable Weld-Logger -->
                        <jarModule>
                            <groupId>org.jboss.weld</groupId>
                            <artifactId>weld-logger</artifactId>
                            <bundleDir>lib</bundleDir>
                        </jarModule>
                        <jarModule>
                            <groupId>javax.enterprise</groupId>
                            <artifactId>cdi-api</artifactId>
                            <bundleDir>lib</bundleDir>
                        </jarModule>

                    </modules>
                </configuration>
            </plugin>

        </plugins>

    </build>

META-INF/ 中的 bean.xml

在没有 ejb 的情况下使用 war in ear 的相同配置(在战争中,但如果我在战争和焊接中使用 ejb - 同样的例外)

Q1:怎么了?

Q2:weld & ejb 3.1的使用规则是什么?

4

1 回答 1

0

提供 Weld Logger 和 SFLF4J 工件,如果您想使用它们,您需要将它们添加到您的应用程序中:

<!-- SL4J API -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.6.0</version>
</dependency>

<!-- SLF4J JDK14 Binding  -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-jdk14</artifactId>
  <version>1.6.0</version>
</dependency>

<!-- Injectable Weld-Logger -->
<dependency>
  <groupId>org.jboss.weld</groupId>
  <artifactId>weld-logger</artifactId>
  <version>1.0.0-CR2</version>
</dependency>

然后

import javax.inject.Inject;
import org.slf4j.Logger;

public class Foo {
    @Inject
    private Logger logger;

    public void bar() {
        logger.info("Look ma, I'm using an injected Logger!");
    }
}

我自己使用焊接记录器(使用 logback 作为绑定)并在 GlassFish 3.0.1 下测试了您的代码,它可以正常工作。

于 2010-07-03T00:33:35.267 回答