CDI在Wildfly 8.1中穿过BDA豆注射

时间:2014-08-24 17:45:07

标签: cdi wildfly weld wildfly-8 weld2

我有一个具有以下结构的EAR文件:

  • LIB
    • jar.jar
      • 测试1
      • 的Test2
  • ejb.jar
    • Test1Impl
  • war.war
    • Test2Impl
    • 的TestServlet

jar.jar包含两个接口

  • 测试1
  • 的Test2

只有在war.war到ejb.jar中有一个清单Class-Path条目时,TestServlet才会注入Test1解析为Test1Impl。 Test1Impl仅在我将ejb.jar中的清单Class-Path条目发送到war.war时才会注入Test2解析为Test2Impl。

Weld documentation的提示条目匹配部署的类加载器结构解释了我需要清单条目的原因。 这种交叉BDA注射应该如何正常工作?添加Class-Path清单条目似乎有点愚蠢,因为实际上我不希望实现可见。我只希望其他子部署中的bean可见。有没有办法做到这一点?

这里的实现

public class Test1Impl implements Test1 {

    @Inject
    private Test2 test2;

    public void hello() {
        System.out.println(test2.getString());
    }

}

public class Test2Impl implements Test2 {

    public String getString() {
        return "Hello";
    }

}

@WebServlet(urlPatterns = "/test")
public class TestServlet implements Servlet {

    @Inject
    private Test1 test;

    public void init(ServletConfig config) throws ServletException {
    }

    public ServletConfig getServletConfig() {
        return null;
    }

    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        test.hello();
    }

    public String getServletInfo() {
        return null;
    }

    public void destroy() {
    }

}

这里是application.xml

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd"
             version="7">
  <description>The EAR</description>
  <display-name>ear</display-name>
  <module>
    <ejb>ejb.jar</ejb>
  </module>
  <module>
    <web>
      <web-uri>war.war</web-uri>
      <context-root>/</context-root>
    </web>
  </module>
  <library-directory>lib</library-directory>
</application>

1 个答案:

答案 0 :(得分:0)

从部署外部使用CDI Bean 一节中的CDI reference所述,需要具有相应依赖关系的jboss-deployment-structure.xml。 虽然这样做解决了我的问题,但我认为CDI规范应该为企业应用程序定义一种可移植的方式。