Search code examples
java-ee-6cdiservlet-filtersejb-3.1

Injecting beans into filter packaged in a shared library


I've got an JavaEE6 app with following structure:

app.ear
  META-INF
    application.xml
  lib
    commmon-server-lib.jar
  webapp1.war
  webapp2.war
  services-ejb.jar

Both webapps have common-server-lib.jar in Class-Path entry of their MANIFEST.MF (skinny war's).

application.xml:

<application>
  <module>
    <ejb>services-ejb.jar</ejb>
  </module>
  <module>
    <web>
      <web-uri>webapp1.war</web-uri>
      <context-root>/webapp1</context-root>
    </web>
  </module>
  <module>
    <web>
      <web-uri>webapp2.war</web-uri>
      <context-root>/webapp2</context-root>
    </web>
  </module>
  <library-directory>lib</library-directory>
</application>

In common-server-lib.jar there is a web filter that should have few EJBs and other CDI managed beans injected. This filter is defined in web.xml's of both webapps.

common-server-lib.jar and war's have beans.xml in appropriate place.

Now the problem is, that when I try to deploy this app to Glassfish I get errors like this:

Class [ Lcom/acme/UserService; ] not found. Error while loading 
    [ class com.acme.filter.MyFilter ]

UserService service is located in services-ejb.jar

So my question is: what am I doing wrong...? Is there something wrong in defining web components (that use injections for it's dependencies) in a shared library?

Edit:

In section 15.5 of JSR-315 (Servlets 3.0 final spec) one can find:

In a web application, classes using resource injection will have their annotations
processed only if they are located in the WEB-INF/classes directory, or if they are
packaged in a jar file located in WEB-INF/lib.

I've moved common-server-lib.jar to WEB-INF/lib directories of both webapps but I'm still having same issue ;/...


Solution

  • After many hours of struggling with this puzzle I've found a solution:

    1. Add web-fragment.xml to commmon-server-lib.jar with my filter
    2. Remove filter specification from webapps web.xml's
    3. Change maven fonfiguration to remove all jar's from WEB-INF/lib directory except commmon-server-lib.jar
    4. Remove commmon-server-lib.jar from EAR /lib directory