Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
README
======

This repository contains simple example projects of various Java 8 based projects.
This repository contains simple example projects of various Java 21 based projects.

All examples are developed and tested using the following software stack:

+ Ubuntu 14.04.1 LTS
+ Oracle Java (JDK) 1.8.0_25
+ NetBeans IDE 8.0.2 (Build 201411181905)
+ Apache Maven 3.2.2
+ Java (JDK) 21
+ Apache Maven 3.9.x or later

49 changes: 20 additions & 29 deletions jetty-maven-cdi/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
README
======

This is an simple setup for testing CDI ([Red Hat JBoss Weld]
This is a 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](http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html)
[jetty-ee10-maven-plugin](https://eclipse.dev/jetty/documentation/jetty-12/programming-guide/index.html)

+ Jetty 9.2.5.v20141112
+ Weld 2.2.7.Final (CDI 1.2)
+ Jetty 12.0.14 (EE10)
+ Weld 5.1.2.Final (CDI 4.0 / Jakarta namespace)

### Running this example Application

+ Check if Java 8 is used
+ Check if Java 21 is used
+ Clone this git repository
+ Go to project directory, `java8-examples`
+ Execute `mvn clean install`
Expand All @@ -19,13 +19,11 @@ This is an simple setup for testing CDI ([Red Hat JBoss Weld]

````bash
$ 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
Apache Maven 3.9.x
Java version: 21, vendor: Oracle Corporation (or equivalent)
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
OS name: "linux", arch: "amd64", family: "unix"
$ git clone https://github.com/COG-GTM/java8-examples.git
Cloning into 'java8-examples'...
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (26/26), done.
Expand Down Expand Up @@ -60,32 +58,25 @@ 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`
+ Add `jakarta.enterprise:jakarta.enterprise.cdi-api:4.0.1`, scope `provided` to your (maven) dependencies
+ Add `org.jboss.weld.servlet:weld-servlet-shaded:5.1.2.Final` as a dependency for
`jetty-ee10-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+](https://issues.jboss.org/browse/WELD-1561)
+ Servlets and Filters
+ Listeners
+ Jetty 12 EE10 requires Weld 5.x+ for CDI 4.0 / Jakarta namespace support
+ Transactional events not available in a non-Java EE environment

### References

+ [JSR 299: Contexts and Dependency Injection for the Java EE platform]
(https://jcp.org/en/jsr/detail?id=299). CDI 1.0, Part of Java EE 6
+ [JSR 346: Contexts and Dependency Injection for Java EE 1.1]
(https://jcp.org/en/jsr/detail?id=346). CDI 1.1, Part of Java EE 7 release and [CDI 1.2]
(http://www.cdi-spec.org/news/2014/04/14/CDI-1_2-released/) maintenance release
+ [The Java EE Tutorial, Contexts and Dependency Injection]
(https://docs.oracle.com/javaee/7/tutorial/partcdi.htm#GJBNR)
+ [Weld - CDI: Contexts and Dependency Injection for the Java EE platform]
(https://docs.jboss.org/weld/reference/latest/en-US/html/index.html)
+ [Must read about CDI 2.0](http://www.next-presso.com/2014/03/forward-cdi-2-0/)
+ [Introduction to JNDI](http://archive.oreilly.com/pub/a/onjava/excerpt/java_servlets_ch12/index.html?page=3)
+ [Working with Jetty JNDI](http://www.eclipse.org/jetty/documentation/current/using-jetty-jndi.html)
+ [CDI 4.0 (Jakarta Contexts and Dependency Injection)](https://jakarta.ee/specifications/cdi/4.0/)
+ [Jakarta EE 10 Specification](https://jakarta.ee/specifications/platform/10/)
+ [Weld 5.x - CDI Reference Implementation](https://docs.jboss.org/weld/reference/latest/en-US/html/index.html)
+ [Jetty 12 Documentation](https://eclipse.dev/jetty/documentation/jetty-12/index.html)
+ [Working with Jetty JNDI](https://eclipse.dev/jetty/documentation/jetty-12/programming-guide/index.html)
36 changes: 13 additions & 23 deletions jetty-maven-cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,16 @@

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

Expand All @@ -39,30 +33,26 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<version>3.4.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.5.v20141112</version>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-maven-plugin</artifactId>
<version>12.0.14</version>
<configuration>
<!--
No need to explicitly configure this if using default name 'jetty-env.xml'
<jettyEnvXml>src/main/webapp/WEB-INF/jetty-env.xml</jettyEnvXml>
-->
<webApp>
<overrideDescriptor>src/main/webapp/WEB-INF/web-overwrite.xml</overrideDescriptor>
<contextXml>src/main/webapp/WEB-INF/jetty-context.xml</contextXml>
</webApp>
<contextXml>src/main/webapp/WEB-INF/jetty-context.xml</contextXml>
</configuration>
<dependencies>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.2.7.Final</version>
<artifactId>weld-servlet-shaded</artifactId>
<version>5.1.2.Final</version>
</dependency>
</dependencies>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package eu.infomas.examples.cdi;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.inject.Inject;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import java.io.IOException;
import java.io.PrintWriter;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jakarta.inject.Inject;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

// Using Servlet 3.0+
@WebServlet(name = "MainServlet", urlPatterns = {"/"})
Expand All @@ -17,7 +17,7 @@ public class MainServlet extends HttpServlet {
@Inject
private Greeting greeting;
@Inject
private javax.enterprise.event.Event<String> event;
private jakarta.enterprise.event.Event<String> event;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
Expand Down
18 changes: 11 additions & 7 deletions jetty-maven-cdi/src/main/webapp/WEB-INF/jetty-context.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure_9_0.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>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://eclipse.dev/jetty/configure_10.dtd">
<Configure class="org.eclipse.jetty.ee10.webapp.WebAppContext">
<Get name="hiddenClassMatcher">
<Call name="exclude">
<Arg>
<Array type="java.lang.String">
<Item>org.eclipse.jetty.ee10.servlet.DecoratingListener</Item>
</Array>
</Arg>
</Call>
</Get>
</Configure>
6 changes: 3 additions & 3 deletions jetty-maven-cdi/src/main/webapp/WEB-INF/jetty-env.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://eclipse.dev/jetty/configure_10.dtd">
<Configure id="wac" class="org.eclipse.jetty.ee10.webapp.WebAppContext">
<New id="BeanManager" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref id="wac" /></Arg>
<Arg>BeanManager</Arg>
<Arg>
<New class="javax.naming.Reference">
<Arg>javax.enterprise.inject.spi.BeanManager</Arg>
<Arg>jakarta.enterprise.inject.spi.BeanManager</Arg>
<Arg>org.jboss.weld.resources.ManagerObjectFactory</Arg>
<Arg/>
</New>
Expand Down
8 changes: 4 additions & 4 deletions jetty-maven-cdi/src/main/webapp/WEB-INF/web-overwrite.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version="6.0">

<env-entry>
<env-entry-name>helloName</env-entry-name>
Expand All @@ -12,7 +12,7 @@

<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-type>jakarta.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>

</web-app>
6 changes: 3 additions & 3 deletions jetty-maven-cdi/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version="6.0">

<!-- References: http://www.eclipse.org/jetty/documentation/current/framework-weld.html
https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html
Expand Down
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Example Java 8 Projects</name>
<name>Example Java 21 Projects</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.release>21</maven.compiler.release>
</properties>

</project>