JBoss EAP 7,仅允许焊接异常 @Dependent

JBoss EAP 7, Weld Exception Only @Dependent is allowed

在尝试将应用程序从 JBoss EAP 5 迁移到 JBoss EAP 以及添加一些额外功能(例如 JAX-RS)时,我收到以下错误:

WELD-000082:class net.MyCompany.My.service.MyIPAuthJaxRsService 的无状态会话 bean 不允许作用域接口 javax.enterprise.context.RequestScoped。只允许@Dependent。

完整堆栈跟踪:

    1:02:29,721 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC000001: Failed to start service jboss.deployment.unit."C1Authentication.ear".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."C1Authentication.ear".WeldStartService: Failed to start service
        at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000082: Scope interface javax.enterprise.context.RequestScoped is not allowed on stateless session beans for class net.MyCompany.My.service.MyIPAuthJaxRsService. Only @Dependent is allowed.
        at org.jboss.weld.bean.SessionBean.checkScopeAllowed(SessionBean.java:122)
        at org.jboss.weld.bean.SessionBean.internalInitialize(SessionBean.java:101)
        at org.jboss.weld.bean.RIBean.initialize(RIBean.java:69)
        at org.jboss.weld.bootstrap.ConcurrentBeanDeployer.doWork(ConcurrentBeanDeployer.java:121)
        at org.jboss.weld.bootstrap.ConcurrentBeanDeployer.doWork(ConcurrentBeanDeployer.java:118)
        at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:63)
        at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:56)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
        at org.jboss.threads.JBossThread.run(JBossThread.java:320)

在 Eclipse 中,项目方面位于 EJB 3.2 和 Java 1.7

我的 EJB-Jar.xml 文件包含:

    <?xml version="1.0" encoding="UTF-8"?>
    <ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd" version="3.2">
        <display-name>C1AuthService</display-name>
        <enterprise-beans>
            <session>
                <ejb-name>MyWSService</ejb-name>
                <ejb-class>net.MyCompany.My.service.MyWSService</ejb-class>
                <session-type>Stateless</session-type>
                <transaction-type>Container</transaction-type>
            </session>
            <session>
                <ejb-name>MyIPAuthJaxRsService</ejb-name>
                <ejb-class>net.MyCompany.My.service.MyIPAuthJaxRsService</ejb-class>
                <session-type>Stateless</session-type>
                <transaction-type>Container</transaction-type>
            </session>
            <session>
                <ejb-name>MyIPAuthJaxRsServiceApplication</ejb-name>
                <ejb-class>net.MyCompany.My.service.MyIPAuthJaxRsServiceApplication</ejb-class>
                <session-type>Stateless</session-type>
                <transaction-type>Container</transaction-type>
            </session>
        </enterprise-beans>
    </ejb-jar>

两个 JAX-RS 类 是:

    package net.mycompany.my.service;

    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    import java.util.HashSet;
    import java.util.Set;

    @ApplicationPath(value="/C1AuthService")
    public class MyIPAuthJaxRsServiceApplication extends Application {

        private Set singletons = new HashSet();

        public MyIPAuthJaxRsServiceApplication() {
            singletons.add(new MyIPAuthJaxRsService());
        }

        @Override
        public Set getSingletons() {
            return singletons;
        }

        @Override
        public Set> getClasses() {
            // TODO Auto-generated method stub
            return null;
        }
    }
    package net.mycompany.my.service;

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;

    @Path("/C1AuthService")
    public class MyIPAuthJaxRsService {
        public MyIPAuthJaxRsService() {
        }

        @GET
        @Path("/test")
        public String test() {
            return "Hello RESTFul 2";
        }

    }

网上搜了一下,发现下面这篇文章提供了线索: https://dzone.com/articles/valid-cdi-scopes-session-ejb

我也偶然发现了这个: http://docs.jboss.org/resteasy/docs/2.0.0.GA/userguide/html/CDI.html

似乎发生在 EJB 中 -Jar.xml 我声明 EJB 是无状态会话 bean 并且不在 java 类 本身中注释它们CDI/Weld 将它们默认为 @Dependent,例如来自 JBoss 文章 A CDI bean that does not explicitly define a scope is @Dependent scoped by default.

只需将以下导入和注释添加到 类:

    package net.mycompany.my.service;

    import javax.ejb.Stateless;
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    import java.util.HashSet;
    import java.util.Set;

    @Stateless
    @ApplicationPath(value="/C1AuthService")
    public class MyIPAuthJaxRsServiceApplication extends Application {

        private Set singletons = new HashSet();

        public MyIPAuthJaxRsServiceApplication() {
            singletons.add(new MyIPAuthJaxRsService());
        }

    }
    package net.mycompany.my.service;

    import javax.ejb.Stateless;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;

    @Stateless
    @Path("/C1AuthService")
    public class MyIPAuthJaxRsService {
        public MyIPAuthJaxRsService() {
        }

        @GET
        @Path("/test")
        public String test() {
            return "Hello RESTFul 2";
        }

    }

基本上检查您是否使用 @Stateless 注释了 EJB,并添加了正确的导入 import javax.ejb.Stateless;

Edit - 正如@SteveC 指出的那样,我已经删除了覆盖但是当我按照他的建议尝试从 MyIPAuthJaxRsServiceApplication 中删除@Stateless 时,我最终再次获得异常MyIPAuthJaxRsServiceApplication。另外值得注意的是,这个例子甚至没有工作,因此 EJB 在 EJB-Jar.xml 中并且注释可能甚至没有必要,我试图在 EAR 中使用 EJB 项目和 jar,注释EJB 与 JAX-RS 但显然它不受支持,因为我在这里与 JBoss 的人一起工作 https://developer.jboss.org/message/972433#972433

看起来您必须有一个用于 JAX-RS 的 servlet 容器,而 EJB 可以有 web 服务注释的事实并不意味着它们正在使用 servlet 容器 - 好吧,我试过了!此外,此问答更多是为了修复 A CDI bean that does not explicitly define a scope is @Dependent scoped by default.,我为此苦苦挣扎了一段时间,并认为如果其他人 运行 遇到同样的问题,我会帮助他们。