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

  • 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 ®