Weld extensions alpha available

Posted by    |       Weld

Weld Extensions is set of portable services and utility classes for CDI which make up a good base layer for extension and application development. You can read more about the sort of portable extensions we are planning in this interview I gave.

Injectable Logger

Weld extensions includes an injectable logger, based on slf4j. By default, the fully qualified class name will be used as the category:

class Sparrow {
   @Inject
   private Logger log;

   ...
}

Alternatively you can specify a category:

class Finch
{
   @Inject @Category("Finch")
   private Logger log;
   
   ...
}

Managed Resource Streams

Weld extensions also contains managed resource streams (the stream is automatically closed for you when the bean goes out of scope) for resources loaded from the classpath:

@RequestScoped
class ResourceClient {
   
   @Inject @Resource("/META-INF/config.xml") 
   InputStream inputStream;

   public void parse() {
      // TODO Parse the inputStream!
   }
}

Here the stream will be closed for you when the request ends. Of course, you often don't know the file name you want to parse at development time, so you can also specify the name at runtime:

@SessionScoped
class ResourceClient {
   
   @Inject ResourceProvider resourceProvider;

   public void parse(String fileName) {
      InputStream is = resourceProvider.loadResourceAsStream(fileName);
      // TODO parse the XML!
   }
}

Here, the stream will be closed for you when the session is destroyed.

At the moment, only loading resources from the classpath is supported, but we'll add support for loading resources from the servlet context soon. As you can see, this can greatly simplyfy using input streams!

Extensions to the core CDI programming model

Gavin has blogged about a number of improvements to the CDI programming model, some of which are available in this release. Included are:

The extensions package is currently available in Maven, and we welcome feedback, either in the Weld forum or in JIRA. Thanks for this release go to David Allen for the injectable logger and to Gavin King and Stuart Douglas for the extensions to the core CDI programming model.


Back to top