SeamFramework.orgCommunity Documentation

Chapter 18. Application servers and environments supported by Weld

18.1. Using Weld with WildFly
18.2. GlassFish
18.3. Servlet containers (such as Tomcat or Jetty)
18.3.1. Tomcat
18.3.2. Jetty
18.3.3. Bean Archive Isolation
18.3.4. Implicit Bean Archive Support
18.4. Java SE
18.4.1. CDI SE Module
18.4.2. Bootstrapping CDI SE
18.4.3. Thread Context
18.4.4. Setting the Classpath
18.4.5. Bean Archive Isolation
18.4.6. Implicit Bean Archive Support
18.5. OSGi

If you are using WildFly 8.0 or better, no additional configuration is required to use Weld (or CDI for that matter).

Weld is also built into GlassFish from V3 onwards. Since GlassFish V3 is the Java EE 6 reference implementation, it must support all features of CDI. What better way for GlassFish to support these features than to use Weld, the CDI reference implementation? Just package up your CDI application and deploy.

While CDI does not require support for servlet environments, Weld can be used in a servlet container, such as Tomcat or Jetty.

Weld can be used as a library in an web application that is deployed to a Servlet container. You should place weld-servlet-shaded.jar within the WEB-INF/lib directory relative to the web root. weld-servlet-shaded.jar is an "uber-jar", meaning it bundles all the bits of Weld and CDI required for running in a Servlet container, for your convenience. Alternatively, you can use its component jars. A list of transitive dependencies can be found in the META-INF/DEPENDENCIES.txt file inside the weld-servlet-shaded.jar artifact.

You also need to explicitly specify the servlet listener (used to boot Weld, and control its interaction with requests) in WEB-INF/web.xml in the web root:


<listener>
   <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>

Actually, you don’t have to register this listener in Servlet 3.x compliant containers which support javax.servlet.ServletContainerInitializer service properly (e.g. Tomcat 7.0.50). In this case org.jboss.weld.environment.servlet.EnhancedListener will do all the necessary work automatically, and what is more important - injection into Listeners will work on some containers as well (see Section 18.3.1, “Tomcat” and Section 18.3.2, “Jetty” for more info).

The downside of javax.servlet.ServletContainerInitializer approach is that the Weld listener will be added to the end of the ordered list of discovered listeners. In practice the request and session context will not be active during ServletRequestListener and HttpSessionListener notifications. Fortunately, it’s possible to combine the EnhancedListener and the old Listener to fix this problem - simply add the old Listener to the web.xml as mentioned above. Note that it must be defined before the listeners using request and session context.

Note

There is quite a special use-case where one more special component must be involved. If you want the session context to be active during HttpSessionListener.sessionDestroyed() invocation when the session times out or when all the sessions are destroyed because the deployment is being removed then org.jboss.weld.servlet.WeldTerminalListener must be specified as the last one in your web.xml. This listener activates the session context before other listeners are invoked (note that the listeners are notified in reverse order when a session is being destroyed).

Since version 2.0.2.Final only Jetty 7, 8 and 9 are supported.

To bind the BeanManager into JNDI, you should either populate WEB-INF/jetty-env.xml with the following contents:


<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
   "http://www.eclipse.org/jetty/configure.dtd">

<Configure id="webAppCtx" class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="BeanManager" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg> <Ref id="webAppCtx"/> </Arg>
        <Arg>BeanManager</Arg>
        <Arg>
            <New class="javax.naming.Reference">
                <Arg>javax.enterprise.inject.spi.BeanManager</Arg>
                <Arg>org.jboss.weld.resources.ManagerObjectFactory</Arg>
                <Arg/>
            </New>
        </Arg>
    </New>
</Configure>

Or you can configure a special Servlet listener to bind the BeanManager automatically:


<listener>
   <listener-class>org.jboss.weld.environment.servlet.BeanManagerResourceBindingListener</listener-class>
</listener>

Just like in Tomcat, you need to make the BeanManager available to your deployment by adding this to the bottom of web.xml:


<resource-env-ref>
   <resource-env-ref-name>BeanManager</resource-env-ref-name>
   <resource-env-ref-type>
      javax.enterprise.inject.spi.BeanManager
   </resource-env-ref-type>
</resource-env-ref>

Jetty only allows you to bind entries to java:comp/env, so the BeanManager will be available at java:comp/env/BeanManager.

Since Jetty 8 the class org.eclipse.jetty.servlet.ServletContextHandler.Decorator is not visible from the web application. Therefore, one more step is required - we have to tell Jetty not to hide this system class.

Simply place the following jetty-context.xml file in the WEB-INF dir:


<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="serverClasses">
        <Array type="java.lang.String">
            <Item>-org.eclipse.jetty.servlet.ServletContextHandler.Decorator</Item>
        </Array>
    </Set>
</Configure>

See also Jetty Classloading documentation.

Weld also supports Servlet and Filter injection in Jetty containers. Listener injection should also work on Jetty 9.1.1 and newer versions.

CDI 1.1 introduced the bean discovery mode of annotated used for implicit bean archives (see also Section 15.6, “Packaging and deployment”). This mode may bring additional overhead during container bootstrap. Therefore, Weld Servlet supports the use of Jandex bytecode scanning library to speed up the scanning process. Simply put the jandex.jar on the classpath. If Jandex is not found on the classpath Weld will use the Java Reflection as a fallback.

In general, an implicit bean archive does not have to contain a beans.xml descriptor. However, such a bean archive is not supported by Weld Servlet, i.e. it’s excluded from discovery.

Note

The bean discovery mode of annotated is supported from version 2.2.5.Final. Previous versions processed implicit bean archives in the same way as explicit bean archives.

In addition to improved integration of the Enterprise Java stack, the "Contexts and Dependency Injection for the Java EE platform" specification also defines a state of the art typesafe, stateful dependency injection framework, which can prove useful in a wide range of application types. To help developers take advantage of this, Weld provides a simple means for being executed in the Java Standard Edition (SE) environment independently of any Java EE APIs.

When executing in the SE environment the following features of Weld are available:

EJB beans are not supported.

CDI SE applications can be bootstrapped in the following ways.

For added flexibility, CDI SE also comes with a bootstrap API which can be called from within your application in order to initialize CDI and obtain references to your application’s beans and events. The API consists of two classes: Weld and WeldContainer.

public class Weld

{
   /** Boots Weld and creates and returns a WeldContainer instance, through which
    * beans and events can be accesed. */
   public WeldContainer initialize() {...}
   /** Convenience method for shutting down the container. */
   public void shutdown() {...}
}
public class WeldContainer

{
   /** Provides access to all beans within the application. */
   public Instance<Object> instance() {...}
   /** Provides access to all events within the application. */
   public Event<Object> event() {...}
   /** Provides direct access to the BeanManager. */
   public BeanManager getBeanManager() {...}
}

Here’s an example application main method which uses this API to initialize a bean of type MyApplicationBean.

import org.jboss.weld.environment.se.Weld;


public static void main(String[] args) {
   Weld weld = new Weld();
   WeldContainer container = weld.initialize();
   container.instance().select(MyApplicationBean.class).get();
   weld.shutdown();
}

Alternatively the application could be started by firing a custom event which would then be observed by another simple bean. The following example fires MyEvent on startup.

org.jboss.weld.environment.se.Weld;


public static void main(String[] args) {
   Weld weld = new Weld();
   WeldContainer container = weld.initialize();
   container.event().select(MyEvent.class).fire( new MyEvent() );
   weld.shutdown();
}

CDI 1.1 introduced the bean discovery mode of annotated used for implicit bean archives (see also Section 15.6, “Packaging and deployment”). This mode may bring additional overhead during container bootstrap. Therefore, Weld Servlet supports the use of Jandex bytecode scanning library to speed up the scanning process. Simply put the jandex.jar on the classpath. If Jandex is not found on the classpath Weld will use the Java Reflection as a fallback.

In general, an implicit bean archive does not have to contain a beans.xml descriptor. However, such a bean archive is not supported by Weld SE, i.e. it’s excluded from discovery.

Note

The bean discovery mode of annotated is supported from version 2.2.0.Final. Previous versions processed implicit bean archives in the same way as explicit bean archives.

Weld supports OSGi environment through Pax CDI. For more information on using Weld in OSGi environment check Pax CDI documentation . In addition, Weld comes with a sample application called Paint which demonstrates how to use CDI with OSGi. Check examples/osgi/README.md for more information.