• 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.
  • Follow Caucho on Twitter

    • Tell us your thoughts about using IDEs with Resin: http://forum.caucho.com/showthread.php?t=16022 2010/08/26
    • Wondering if a CDI-based alternative to grails/roo has appeared. A groovy/CDI framework would be interesting. 2010/08/24
    • By open source experience, I mean contributions 2010/08/17
  • Tags

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

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
« websockets and pong
Put Resin statistics graphs into anything using REST »

replacing env-entry with CanDI @Named

Since the JavaEE 6 Web Profile is our main focus for the next few months, I’m going through the JavaEE resource and environment configuration and injection, including the new JavaEE 6 JNDI system. In many cases, though, using CanDI as a registration blackboard is a cleaner, simpler and more type-safe system than using JNDI. Since we believe spending the effort on clean code pays off, the new type-safe capabilities are worth a look.

For example, JavaEE has an <env-entry> tag, which configures literals like Strings and integers. We can replace those with a as follows, and inject a servlet with our configured values:

MyServlet injecting a config var

import javax.inject.*;
import javax.servlet.*;
import java.io.*;

public class MyServlet extends GenericServlet {
  @Inject @Named("my-var")
  private String _myValue;

  public void service(ServletRequest req, ServletResponse res)
    throws IOException, ServletException
  {
    PrintWriter out = res.getWriter();

    out.println("custom value: " + _myValue);
  }
}

The configuration using CanDI in the resin-web.xml looks like the following:

resin-web.xml configuring the var

<web-app xmlns="http://caucho.com/ns/resin"
    xmlns:lang="urn:java:java.lang"
    xmlns:ee="urn:java:ee">

  <lang:String>
    <new>my-value</new>
    <ee:Named>my-var</ee:Named>
  </lang:String>

</web-app>

The String is instantiated with <lang:String> with a <new> tag for the constructor. The xmlns:lang tells CanDI the package for java.lang.

The old <env-entry> still works (and the old @Resource works too, but you should start migrating to @Inject). For reference, the env-entry looks like the following:

resin-web.xml configuring the var

<web-app xmlns="http://caucho.com/ns/resin">

  <env-entry>
    <env-entry-name>my-var</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>my-value</env-entry-value>
  </env-entry>

</web-app>

This entry was posted on Monday, January 11th, 2010 at 11:55 am and is filed under Uncategorized. 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.

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