Simplest Possible POJO Injection Example With Java EE 6 📎
javax.inject.Inject
:
@Stateless
public class Cart {
@Inject
OrderSystem ordering;
@Inject
CustomerNotification notifier;
public void checkout(){
ordering.placeOrder();
notifier.sendNotification();
}
}
The existence of a no-arg constructor is sufficient:
public class OrderSystem {
public void placeOrder(){
}
}
You only need the beans.xml file in the [WAR]/WEB-INF folder with the content: <beans/>
[It is an excerpt from the project EJBAndCDI - it was pushed into http://kenai.com/projects/javaee-patterns.
The size of the WAR is 16kB (with JSF) and the initial deployment took 800ms (...somehow slow today :-)).]