CDI cross BDA bean injection in Wildfly 8.1

855 views Asked by At

I have an EAR file with the following structure:

  • lib
    • jar.jar
      • Test1
      • Test2
  • ejb.jar
    • Test1Impl
  • war.war
    • Test2Impl
    • TestServlet

The jar.jar contains two interfaces

  • Test1
  • Test2

The TestServlet injects Test1 which resolves to Test1Impl only if I have a manifest Class-Path entry in war.war to ejb.jar. Test1Impl injects Test2 which resolves to Test2Impl only if I have a manifest Class-Path entry in ejb.jar to war.war.

The tip entry Matching the classloader structure for the deployment of the Weld documentation explains why I need the manifest entries. How is this cross BDA injection supposed to work normally? Adding Class-Path manifest entries seems a bit stupid because actually I don't want the implementations to be visible. I only want that the beans from other subdeployments to be visible. Is there any way to do that?

Here the implementations

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() {
    }

}

And here the 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

There are 1 answers

2
Christian Beikov On

As described in the CDI reference in the section Using CDI Beans from outside the deployment a jboss-deployment-structure.xml with the appropriate dependencies is required. Although doing this fixed my problems, I think that the CDI spec should define a portable way for doing this for enterprise applications.