Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

README

This is an simple setup for testing CDI ([Red Hat JBoss Weld] (https://docs.jboss.org/weld/reference/latest/en-US/html/)) and Jetty using the jetty-maven-plugin

  • Jetty 9.2.5.v20141112
  • Weld 2.2.7.Final (CDI 1.2)

Running this example Application

  • Check if Java 8 is used
  • Clone this git repository
  • Go to project directory, java8-examples
  • Execute mvn clean install
  • Go to root directory of this subproject, jetty-maven-cdi
  • Start Jetty and go to http://localhost:8080/ to view the result in your browser
$ mvn --version
Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 2014-06-17T15:51:42+02:00)
Maven home: /usr/share/maven/apache-maven-3.2.2
Java version: 1.8.0_25, vendor: Oracle Corporation
Java home: /usr/lib/jvm/jdk1.8.0_25/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.13.0-43-generic", arch: "amd64", family: "unix"
$ git clone https://github.com/rmuller/java8-examples.git
Cloning into 'java8-examples'...
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (26/26), done.
remote: Total 43 (delta 4), reused 38 (delta 2)
Unpacking objects: 100% (43/43), done.
Checking connectivity... done.
$ cd java8-examples/
$ mvn clean install
[INFO] Scanning for projects...
...
$ cd jetty-maven-cdi/
$ mvn jetty:run
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building jetty-maven-cdi 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
2014-12-26 15:49:20.915:INFO:oejs.ServerConnector:main: Started ServerConnector@2a685eba{HTTP/1.1}{0.0.0.0:8080}
2014-12-26 15:49:20.916:INFO:oejs.Server:main: Started @3828ms
[INFO] Started Jetty Server

Jetty configuration

To enable CDI, you need to configure Jetty first. Several online resources describe the configuration in a different way. Most importantly the official Jetty and Weld documentation are not consistent.

How to setup a CDI enabled application?

  • Add javax.enterprise:cdi-api:1.2, scope provided to your (maven) dependencies
  • Add org.jboss.weld.servlet:weld-servlet:2.2.7.Final as a dependency for jetty-maven-plugin
  • Managed beans must have a default constructor and may not be final (must be proxiable)
  • Managed beans declaring a passivating scope must be passivation capable, implement java.io.Serializable and all @Interceptors must be Serializable as well

Notes

  • CDI injection is available in
    • Servlets and Filters (Jetty 7.2+)
    • Listeners (Jetty 9.1.1+)
  • Jetty 9.1.0+ requires Weld 2.2.0+
  • Transactional events not available in a non-Java EE environment

References