SeamFramework.orgCommunity Documentation

Chapter 18. Application servers and environments supported by Weld

18.1. Using Weld with JBoss AS
18.2. GlassFish
18.3. Servlet containers (such as Tomcat or Jetty)
18.3.1. Tomcat
18.3.2. Jetty
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

If you are using JBoss AS 6.0, no additional configuration is required to use Weld (or CDI for that matter). All you need to do is make your application a bean bean archive by adding META-INF/beans.xml to the classpath or WEB-INF/beans.xml to the web root!

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 JSR-299 reference implementation? Just package up your CDI application and deploy.

While JSR-299 does not require support for servlet environments, Weld can be used in a servlet container, such as Tomcat 6.0 or Jetty 6.1.

Weld can be used as a library in an web application that is deployed to a Servlet container. You should place weld-servlet.jar within the WEB-INF/lib directory relative to the web root. weld-servlet.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.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>

Like Tomcat, Jetty has a read-only JNDI, so Weld can't automatically bind the BeanManager. To bind the BeanManager to JNDI in Jetty 6, you should populate WEB-INF/jetty-env.xml with the following contents:


<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
   "http://jetty.mortbay.org/configure.dtd">
<Configure id="webAppCtx" class="org.mortbay.jetty.webapp.WebAppContext">
   <New id="BeanManager" class="org.mortbay.jetty.plus.naming.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>

Jetty 7 has moved to the Eclipse Foundation; if you are using Jetty 7 put the following in your WEB-INF/jetty-env.xml:


<!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> 

Just like in Tomcat, you need to make it 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>

Notice that Jetty doesn't not have built-in support for an javax.naming.spi.ObjectFactory like Tomcat, so it's necessary to manually create the javax.naming.Reference to wrap around it.

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

Weld also supports Servlet injection in Jetty 6. To enable this, add the file META-INF/jetty-web.xml with the following content to your war:


<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
   "http://jetty.mortbay.org/configure.dtd">
<Configure id="webAppCtx" class="org.mortbay.jetty.webapp.WebAppContext">
   <Call class="org.jboss.weld.environment.jetty.WeldServletHandler" name="process">
      <Arg><Ref id="webAppCtx"/></Arg>
   </Call>
</Configure>

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.

public static void main(String[] args) {

   WeldContainer weld = new Weld().initialize();
   weld.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.

public static void main(String[] args) {

   WeldContainer weld = new Weld().initialize();
   weld.event().select(MyEvent.class).fire( new MyEvent() );
   weld.shutdown();
}