Java CanDI (JSR-299 Injection)

The latest draft of JSR-299 for Java Injection is now available, and we’ve been madly repackaging and renaming our test cases to follow. The biggest change is the name of the spec: Java Contexts and Dependency Injection (Java CanDI), instead of the old WebBeans. (The image at the right is supposed to be a Resin lollipop.)
Along with a new Resin 4.0 snapshot, I’ve done a quick pass at the Resin Injection documentation
and updated the JavaDoc.
A quick overview of the new packaging and examples follows:
Packages
- javax.inject has the annotations and exceptions for Java Injection.
- javax.inject.manager has the programmatic interface to the Manager classes.
- javax.context contains the contexts and scoping (@ApplicationScoped, @SessionScoped, etc)
- javax.interceptor contains the method-based AOP interception
- javax.decorator contains the interface-based AOP decoration
- javax.event contains event throwing and observing
- javax.annotation contains important general annotations @Named and @Stereotype
Example: Hello, World
For the hello, world, I’m creating a Hello.java bean and injecting it into a Servlet. The Servlet injection is now part of the spec.
- example/Hello.java - the Java class to inject
- example/HelloServlet.java - the Servlet which needs Hello
- META-INF/beans.xml - trivial file to trigger Java injection scanning
XML and classpath scanning
The presence of a META-INF/beans.xml in a classpath triggers scanning of all classes for Java Injection registration. For example, jar WEB-INF/classes/META-INF/beans.xml. Because no special annotation is required, all Java classes are automatically injectable beans. For hello, world, I’ll just create a trivial file:
classes/META-INF/beans.xml
<Beans xmlns="urn:java:ee"> </Beans>
The “urn:java:ee” is an imporant context in Java Injection because it refers to the Java package “ee” directly. “ee” imports a bunch of other packages like javax.inject and java.util, which saves on the namespace declarations. For hello, world, we can ignore that and cut and paste.
The META-INF/beans.xml triggers scanning of all classes in that jar or directory, so it will find my Hello.java.
Hello.java
Hello.java is a plain Java object with no special annotations.
package example;
public class Hello {
public String toString()
{
return "Hello[hello, world]";
}
}
HelloServlet.java
The HelloServlet uses a single injection annotation from javax.inject, @Current. Since I have a single unique Hello.java class, the @Current annotation will grab it. If I had multiple Hello beans, possibly with different configurations, I’d need to use a more specific @BindingType. But that’s for later. This is just Hello, World.
package example;
import java.io.*;
import javax.inject.Current;
import javax.servlet.*;
public class HelloServlet extends GenericServlet {
@Current
private Hello _hello;
public void service(ServletRequest req, ServletResponse res)
throws IOException
{
PrintWriter out = res.getWriter();
out.println(_hello);
}
}
And I’m done. Three files total (plus the web.xml for the servlet), and I’ve injected a trivial bean into my servlet.
In a future Hello, World, I’ll show how to export the Hello.java to a JSP or JSF or PHP page. (Basically, the @Named annotation with JSP EL.)

January 28th, 2009 at 5:06 pm
This is great work you’re doing.
But I couldn’t get the example to work. Using 4.0 snapshot 01/27/09 error:
example.HelloServlet._hello: Can’t find a component for ‘example.Hello’ because no beans match
I don’t expect to get help here just didn’t want to file a bug report on alpha features
January 28th, 2009 at 5:13 pm
Oh just wanted to say I did get this to work. I had put the META-INF/beans.xml in the example package rather than in WEB-INF/classes. Sorry
Anyway great work again. By far the easiest documentation I’ve found to follow. Looking forward to more examples!
January 28th, 2009 at 8:53 pm
Thanks! The spec is full of great stuff, so it can be really tempting to show off all the fancy things it can do instead of explaining the core concepts.