在Wildfly 8.1中注入CDI交叉BDA bean



我有一个EAR文件,其结构如下:

  • 自由
    • jar.jar
        Test1
      • Test2
  • ejb.jar
    • Test1Impl
  • war.war
    • Test2Impl
    • TestServlet
jar.jar包含两个接口
    Test1
  • Test2

TestServlet注入Test1,只有当我在war中有一个manifest类路径条目时,它才会解析为Test1Impl。War到ejb.jar。Test1Impl注入Test2,只有当我在ejb.jar中有一个manifest类路径条目war.war时,它才会解析为Test2Impl。

提示条目与Weld文档的部署的类加载器结构相匹配,解释了为什么我需要清单条目。这种跨BDA注入应该如何正常工作?添加类路径清单条目似乎有点愚蠢,因为实际上我不希望实现是可见的。我只希望其他子部署中的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>

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

最新更新