main website home
  • About this blog

    This blog features updates, opinions, and technical notes from Caucho engineers about Caucho products, the enterprise Java industry, and PHP. Caucho Technology is the creator of the Resin Application Server and the Quercus PHP in Java engine. A leader in Java performance since 1998, Caucho is a Sun JavaEE licensee with over 9000 customers worldwide.
  • Tags

    ajaxworld bam candi cdi cloud cluster comet configuration deploy devoxx eclipse ejb embedded flash flex google app engine hessian hmtp ioc java ee 6 javaone javazone jms messaging newsletter nyjug osgi php pomegranate quercus resin resin 4.0 REST servlet sfjug silicon valley code camp spring testing training tssjs watchdog webbeans web profile websockets wordpress
  • Meta

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
« Resin Clustered Deployment
Remote deploy in Resin, Part 2 »

Java CanDI (JSR-299 Injection)

Resin CanDI

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.)

Tags: candi, core, injection, ioc, resin

This entry was posted on Wednesday, January 28th, 2009 at 1:53 pm and is filed under Engineering. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Responses to “Java CanDI (JSR-299 Injection)”

  1. atomi Says:
    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 :)

  2. atomi Says:
    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!

  3. ferg Says:
    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.

Leave a Reply

You must be logged in to post a comment.


Caucho Technology is proudly powered by WordPress and Quercus®
Entries (RSS) and Comments (RSS).

  • HOME |
  • CONTACT US |
  • DOCUMENTATION |
  • BLOG |
  • WIKI 4 |
  • WIKI 3 |
  • Resin: Java Application Server
Copyright (c) 1998-2012 Caucho Technology, Inc. All rights reserved.
caucho® , resin® and quercus® are registered trademarks of Caucho Technology, Inc.
resin® is a cloud optimized, java® application server that supports the java ee webprofile ®