Jetty Logo
Version: 9.4.32.v20200930
Contact the core Jetty developers at www.webtide.com

private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services for sponsored feature development

Chapter 29. Frameworks

Table of Contents

CDI
Weld
Spring Setup
OSGI
Metro

CDI

Contexts and Dependency Injection for Java EE (CDI) is a standard implemented by frameworks such as Weld and Apache OpenWebBeans. This is a common way to assemble and configure webapplications by a process often referred to as decoration.

Jetty integration of CDI frameworks allows CDI to be used to inject the Filters, Servlets and Listeners created within a Servlet Context. There are two approaches to integration:

  • CDI implementation can integrate with Jetty. This requires the CDI implementation to have Jetty specific code. Since Jetty-9.4.20 a loosely bound mechanism has been available for CDI implementations to extends the Jetty DecoratedObjectFactory without hard API dependencies. Prior to that, CDI implementations directly called jetty APIs that need to be explicitly exposed to the webapp.
  • Alternately, Jetty can integrate with CDI implementations by using standard CDI SPIs.

Jetty CDI Modules

The Jetty distribution come with several CDI modules. These modules do not provide CDI, but instead enable one of more integration mechanisms.

Jetty cdi Module

The cdi module supports either two modes of CDI integration which can be selected either by the "org.eclipse.jetty.cdi" context init parameter or the "org.eclipse.jetty.cdi" server attribute (which is initialised from the "jetty.cdi.mode" start property). Supported modes are:

  • CdiSpiDecorator Jetty will call the CDI SPI within the webapp to decorate objects (default).
  • CdiDecoratingLister The webapp may register a decorator on the context attribute "org.eclipse.jetty.cdi.decorator".
cd $JETTY_BASE
java -jar $JETTY_HOME/start.jar --add-to-start=cdi

Jetty cdi-decorate Module

This module depends on the cdi module and sets the default mode to CdiDecoratingListener. This is the preferred mode for Weld integration.

cd $JETTY_BASE
java -jar $JETTY_HOME/start.jar --add-to-start=cdi-decorate

Jetty cdi-spi Module

This module depends on the cdi module and sets the default mode to CdiSpiDecorator. This is the preferred mode for Open Web Beans integration.

cd $JETTY_BASE
java -jar $JETTY_HOME/start.jar --add-to-start=cdi-spi

Jetty cdi2 Module

This module supports the deprecated technique of exposing private Jetty decorate APIs to the CDI implementation in the webapp.

cd $JETTY_BASE
java -jar $JETTY_HOME/start.jar --add-to-start=cdi2

This module is equivalent to directly modifying the class path configuration with a jetty-web.xml like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
   <Call name="prependServerClass">
      <Arg>-org.eclipse.jetty.util.Decorator</Arg>
   </Call>
   <Call name="prependServerClass">
      <Arg>-org.eclipse.jetty.util.DecoratedObjectFactory</Arg>
   </Call>
   <Call name="prependServerClass">
      <Arg>-org.eclipse.jetty.server.handler.ContextHandler.</Arg>
   </Call>
   <Call name="prependServerClass">
      <Arg>-org.eclipse.jetty.server.handler.ContextHandler</Arg>
   </Call>
   <Call name="prependServerClass">
      <Arg>-org.eclipse.jetty.servlet.ServletContextHandler</Arg>
   </Call>
</Configure>

Tip

The cdi2 module or directly modifying the web application classpath will not work for Jetty 10.0.0 and later. It should only be used for versions prior to Jetty 9.4.20 and/or Weld 3.1.2.Final

Embedded Jetty with CDI

When starting embedded Jetty programmatically from the main method, to use CDI it may be necessary:

  • enable a Jetty CDI integration mode
  • and/or enable a CDI frame integration.

However, depending on the exact configuration of the embedded server, either or both steps may not be required as `ServletContainerInitializer`s may be discovered.

The details for embedding CDI is explained in the Embedded Jetty with Weld section, which can also be adapted to other CDI frameworks.

See an error or something missing? Contribute to this documentation at Github!(Generated: 2020-09-30)