CDI没有配置到glassfish v3.0.1

时间:2010-07-02 16:24:39

标签: java-ee glassfish cdi ejb-3.1 java-ee-6

使用焊接记录仪注射时遇到一些问题。

情景:我内心有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 |警告|甲骨文glassfish3.0.1 | javax.enterprise.system.container.ejb.com.sun.ejb.containers | _ThreadID = 48; _ThreadName = Thread- 1; | A   期间发生系统异常   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.deliverTimeout(EJBTimerService.java:1667)     在   com.sun.ejb.containers.EJBTimerService.access $ 100(EJBTimerService.java:98)     在   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:不能   创建无状态EJB   com.sun.ejb.containers.StatelessSessionContainer $ SessionContextFactory.create(StatelessSessionContainer.java:720)     在   com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:200)     在   com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:443)     ... 12更多引起:   javax.ejb.CreateException:不能   创建无状态EJB   com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:528)     在   com.sun.ejb.containers.StatelessSessionContainer.access $ 000(StatelessSessionContainer.java:90)     在   com.sun.ejb.containers.StatelessSessionContainer $ SessionContextFactory.create(StatelessSessionContainer.java:718)     ... 14更多引起:   java.lang.NullPointerException at   java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:768)     在   org.jboss.weld.manager.BeanManagerImpl.getBean(BeanManagerImpl.java:1171)     在   org.jboss.weld.manager.BeanManagerImpl.getBean(BeanManagerImpl.java:132)     在   org.glassfish.weld.services.JCDIServiceImpl._createJCDIInjectionContext(JCDIServiceImpl.java:145)     在   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更多|#]

使用: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>

ear 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 war(在战争中,但如果我在战争和焊接中使用ejb - 同样的例外)

Q1:出了什么问题?

Q2:使用焊接和使用的规则是什么? ejb 3.1?

2 个答案:

答案 0 :(得分:0)

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

<!-- 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下测试您的代码,它只是工作。

答案 1 :(得分:0)