Weld tutorial – Part 2


Welcome to part 2 of my litte Weld tutorial. Before I start I have to point you to the PFD2 of Weld. Why? Well, the whole JSR-299 is very extensive. It is IMO nearly impossible to cover all parts of it in a few lines. Hence I’m not going into much details of Weld but just show you how to get started.

Having said that lets go to…

Obtaining the BeanManager

In your web.xml/context.xml you declared a managed resource called BeanManager which is main interface to all Bean related stuff (adding/removing beans from a context etc.). There are two ways for obtaining the Bean Manager. The first way is to use dependey injection with the @Resource annotation:

@Resource(mappedName="java:/comp/env/BeanManager")
BeanManager beanManager;

Here you let the container do all the work. Or you cann ask Weld to inject the BeanManager by using @Inject:

@Inject
BeanManager beanManager;

Simple Dependency Injection

In the previous section I made a little trip to the future by using the @Inject annotation. As you might have already guessed this annotation tells Weld that the field/parameter should be injected. This is the simplest dependency injection available. You just tell Weld that you need something injected and Weld takes care of it. A good example for this would be a Cache which is typically application scoped (although every scoped bean can be injected):

package de.publicstaticfinal.web.jsf;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named
@ApplicationScoped
public class Cache {...}

As you learned already this managed bean can be accessed via EL using #{cache}. But most often you’re not only using the Cache in the frontend but in the business logic too. The extenden HelloWeld bean would look like this:

package de.publicstaticfinal.web.jsf;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named("helloWeld")
@SessionScoped
public class HelloWeld implements Serializable {
  private String message = "Hello Weld";

  @Inject
  private transient Cache cache;

  public String getMessage() {
    return message;
  }
}

The injected Cache object has to be marked as transient since HelloWeld is serializable while the Cache is not. If the cache field is not marked as transient Weld will throw an Exception since it enforces serializability. This is really a good feature in Weld since it leads to consistent data.

Another good point is that you can use loose coupling without much effort. If the Cache class above would be “CacheImpl” and implementing a Cache interface you don’t have to change your code that much. The Cache implementation class would look like this:

package de.publicstaticfinal.web.jsf;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named("cache")
@ApplicationScoped
public class CacheImpl implements Cache
{

}

And the CacheImpl would be injected like this:

@Inject @Named("cache")
private transient Cache cache;

What if you need mor than one Caches? I’m too lazy at the moment to think of a scenario where you might need this but of course this is possible. Although with the restriction that this cannot be achieved directly by dependency injection. If you asked yourself why I explained how to obtain the BeanManager here’s the answer: the BeanManager helps you to create new instances of an injectable resource. The following mehtod gets called with getNewCache(“cache”):

private Cache getNewCache(String elName) {
  Set<Bean<?>> beans = beanManager.getBeans(elName);
  for (Bean<?> bean : beans) {
    Set<Type> beanTypes = bean.getTypes();
    for (Type type : beanTypes) {
      if (type.equals(Cache.class)) {
        Bean<Cache> tmp = (Bean<Cache>) bean;
        Cache newCache = tmp.create(beanManager.createCreationalContext(tmp));
        return newCache;
      }
    }
 }
 return null;
}
  • 2: In this line all beans which can be accessed by #{cache} are retrieved
  • 4: all types of the bean are read
  • 6: Check if the bean is of type Cache
  • 7: cast to the proper type
  • 8: retrieval of the proxy object by api calls

I’m not going into detail of every single API call. If you’re interested in more information please refer to the Javadoc (link: http://docs.jboss.org/cdi/api/1.0-CR1/).

I think that’s enough for the second part of my tutorial. This part just covered injection of scoped beans. While this is a pretty powerful tool JSR-299 doesn’t limit injection to scoped beans. Generally speaking you can blow inject your foot away using Weld.

, , ,

  1. No comments yet.
(will not be published)
Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 163 bad guys.