将CDI项目作为库添加到Eclipse中的EAR

时间:2016-03-11 12:28:43

标签: eclipse java-ee cdi wildfly

我正在Eclipse Mars和WildFly 8.2中进行一些CDI测试。

我有一个动态Web项目(作为EAR的模块)ItsProjectWEB托管一个简单的会话范围的bean

@SessionScoped
public class Cart implements Serializable {

    private static final long serialVersionUID = 1L;

    public Cart() {}

    List<String> list = new ArrayList<>();

    public List<String> addAndGet(String item){
        list.add(item);
        return list;
    }

}

以及注入和调用bean的servlet

@WebServlet("/ItsServlet")
public class ItsServlet extends HttpServlet {

    @Inject Cart cart;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        StringBuilder text=new StringBuilder();
        text.append("Cart = "+cart.addAndGet("Item #"+LocalTime.now().getSecond()));
        text.append("\nServed at: "+request.getContextPath());

        response.getWriter().append(text);
    }

    //irrelevant code
}

一切正常,这是项目的结构:

enter image description here

我的想法是创建一个单独的动态Web项目,放置CDI bean,将此项目添加到我的EAR,并能够从其他模块中引用它。这就是我所做的:

  1. 通过复制第一个
  2. 的结构构建了一个单独的项目ItsProjectCDI

    enter image description here

    1. 将此项目添加到ItsProjectWEB的Java构建路径(右键单击 - &gt; Java构建路径 - &gt;项目 - &gt;添加...)

    2. 尝试通过@Inject ItsCart cart;

    3. 将bean注入servlet中

      但是我在服务器日志中得到了这个错误

      13:22:46,089 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "ItsProjectEAR.ear" (runtime-name: "ItsProjectEAR.ear")
      13:22:46,095 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015973: Starting subdeployment (runtime-name: "ItsProjectCDI.war")
      13:22:46,096 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015973: Starting subdeployment (runtime-name: "ItsProjectWEB.war")
      13:22:46,184 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) JBAS016002: Processing weld deployment ItsProjectEAR.ear
      13:22:46,197 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) JBAS016002: Processing weld deployment ItsProjectCDI.war
      13:22:46,223 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC000001: Failed to start service jboss.deployment.subunit."ItsProjectEAR.ear"."ItsProjectWEB.war".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.subunit."ItsProjectEAR.ear"."ItsProjectWEB.war".POST_MODULE: JBAS018733: Failed to process phase POST_MODULE of subdeployment "ItsProjectWEB.war" of deployment "ItsProjectEAR.ear"
          at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:166) [wildfly-server-8.2.1.Final.jar:8.2.1.Final]
          at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
          at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
          at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.8.0_45]
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.8.0_45]
          at java.lang.Thread.run(Unknown Source) [rt.jar:1.8.0_45]
      Caused by: java.lang.RuntimeException: JBAS018757: Error getting reflective information for class com.mui.servlet.ItsServlet with ClassLoader ModuleClassLoader for Module "deployment.ItsProjectEAR.ear.ItsProjectWEB.war:main" from Service Module Loader
          at org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex.getClassIndex(DeploymentReflectionIndex.java:72) [wildfly-server-8.2.1.Final.jar:8.2.1.Final]
          at org.jboss.as.ee.metadata.MethodAnnotationAggregator.runtimeAnnotationInformation(MethodAnnotationAggregator.java:58)
          at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.handleAnnotations(InterceptorAnnotationProcessor.java:107)
          at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.processComponentConfig(InterceptorAnnotationProcessor.java:92)
          at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.deploy(InterceptorAnnotationProcessor.java:77)
          at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:159) [wildfly-server-8.2.1.Final.jar:8.2.1.Final]
          ... 5 more
      Caused by: java.lang.NoClassDefFoundError: Lcom/mui/cdi/ItsCart;
          at java.lang.Class.getDeclaredFields0(Native Method) [rt.jar:1.8.0_45]
          at java.lang.Class.privateGetDeclaredFields(Unknown Source) [rt.jar:1.8.0_45]
          at java.lang.Class.getDeclaredFields(Unknown Source) [rt.jar:1.8.0_45]
          at org.jboss.as.server.deployment.reflect.ClassReflectionIndex.<init>(ClassReflectionIndex.java:57) [wildfly-server-8.2.1.Final.jar:8.2.1.Final]
          at org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex.getClassIndex(DeploymentReflectionIndex.java:68) [wildfly-server-8.2.1.Final.jar:8.2.1.Final]
          ... 10 more
      Caused by: java.lang.ClassNotFoundException: com.mui.cdi.ItsCart from [Module "deployment.ItsProjectEAR.ear.ItsProjectWEB.war:main" from Service Module Loader]
          at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:213) [jboss-modules.jar:1.3.3.Final]
          at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:459) [jboss-modules.jar:1.3.3.Final]
          at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:408) [jboss-modules.jar:1.3.3.Final]
          at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:389) [jboss-modules.jar:1.3.3.Final]
          at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:134) [jboss-modules.jar:1.3.3.Final]
          ... 15 more
      
      13:22:46,232 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 1) JBAS014613: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {
          "JBAS014671: Failed services" => {"jboss.deployment.subunit.\"ItsProjectEAR.ear\".\"ItsProjectWEB.war\".POST_MODULE" => "org.jboss.msc.service.StartException in service jboss.deployment.subunit.\"ItsProjectEAR.ear\".\"ItsProjectWEB.war\".POST_MODULE: JBAS018733: Failed to process phase POST_MODULE of subdeployment \"ItsProjectWEB.war\" of deployment \"ItsProjectEAR.ear\"
          Caused by: java.lang.RuntimeException: JBAS018757: Error getting reflective information for class com.mui.servlet.ItsServlet with ClassLoader ModuleClassLoader for Module \"deployment.ItsProjectEAR.ear.ItsProjectWEB.war:main\" from Service Module Loader
          Caused by: java.lang.NoClassDefFoundError: Lcom/mui/cdi/ItsCart;
          Caused by: java.lang.ClassNotFoundException: com.mui.cdi.ItsCart from [Module \"deployment.ItsProjectEAR.ear.ItsProjectWEB.war:main\" from Service Module Loader]"},
          "JBAS014771: Services with missing/unavailable dependencies" => [
              "jboss.deployment.unit.\"ItsProjectEAR.ear\".weld.weldClassIntrospector is missing [jboss.deployment.unit.\"ItsProjectEAR.ear\".beanmanager]",
              "jboss.deployment.subunit.\"ItsProjectEAR.ear\".\"ItsProjectCDI.war\".weld.weldClassIntrospector is missing [jboss.deployment.subunit.\"ItsProjectEAR.ear\".\"ItsProjectCDI.war\".beanmanager]"
          ]
      }
      13:22:46,312 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018565: Replaced deployment "ItsProjectEAR.ear" with deployment "ItsProjectEAR.ear"
      13:22:46,315 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 1) JBAS014774: Service status report
      JBAS014777:   Services which failed to start:      service jboss.deployment.subunit."ItsProjectEAR.ear"."ItsProjectWEB.war".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.subunit."ItsProjectEAR.ear"."ItsProjectWEB.war".POST_MODULE: JBAS018733: Failed to process phase POST_MODULE of subdeployment "ItsProjectWEB.war" of deployment "ItsProjectEAR.ear"
            service jboss.deployment.subunit."ItsProjectEAR.ear"."ItsProjectWEB.war".POST_MODULE
      

      我无法理解这里发生的事情,新项目与旧项目相同,但注射不会成功。我做错了什么?

0 个答案:

没有答案