Posts Tagged ‘CDI’
  • My case against autowiring

    Autowiring is a particular kind of wiring, where injecting dependencies is not explicit but actually managed implicitly by the container. This article tries to provide some relevant info regarding disadvantages of using autowiring. Although Spring is taken as an example, the same reasoning can apply to JavaEE’s CDI.

    Autowiring basics

    Autowiring flavors

    Autowiring comes into different flavors:

    • Autowiring by type means Spring will look for available beans that match the type used in the setter. For example, for a bean having a setCar(Car car) method, Spring will look for a Car type (or one of its subtype) in the bean context.
    • Autowiring by name means Spring search strategy is based on beans name. For the previous example, Spring will look for a bean named car regardless of its type. This both requires the bean to be named (no anonymous bean here) and implies that the developer is responsible for any type mistmatch (if the car bean is of type Plane).

    Autowiring is also possible for constructor injection.

    Autowiring through XML configuration

    Let’s first dispel some misconceptions: autowiring does not imply annotations. In fact, autowiring is available through XML since ages and can be enabled with the following syntax.

    <bean autowire="byType" class="some.Clazz" />

    This means Spring will try to find the right type to fill the dependency(ies) by using setter(s).

    Alternative autowire parameters include:

    • by name: instead of matching by type, Spring will use bean names
    • constructor: constructor injection restricted to by-type
    • autodetect: use constructor injection, but falls back to by-type if no adequate constructor is found

    Autowiring through annotations configuration

    Available annotations are the following:

    Annotation Description Source


    Wires by type

    Spring 2.5+


    Wires by name

    Java EE 5+


    Wires by type

    Java EE 6+


    Narrows type wiring

    Java EE 6+

    Dependency Injection is all about decoupling your classes from one another to make them more testable. Autowiring through annotations strongly couples annotated classes to annotations libraries. Admittedly, this is worse for Spring’s @Autowired than for others as they are generally provided by application servers.

    Why it is so bad after all?

    Autowiring is all about simplifying dependency injection, and although it may seem seductive at first, it’s not maintainable in real-life projects.

    Explicit dependency injection is - guess what, explicitly defining a single bean to match the required dependency. Even better, if Java Configuration is used, it is validated at compile time.

    On the opposite, autowiring is about expressing constraints on all available beans in the Spring context so that one and only one matches the required dependency. In effect, you delegate wiring to Spring, tasking him to find the right bean for you. This means that by adding beans in your context, you run the risk of providing more than one match, if your previous constraints do not apply to the new beans. The larger the project, the larger the team (or even worse, the number of teams), the larger the context, the higher the risk.

    For example, imagine you crafted a real nice service with a single implementation, that you need to inject somewhere. After a while, someone notices your service but creates an alternative implementation for some reason. By just declaring this new bean in the context, along with the former, it will break container initialization! Worse, it may be done in an entirely different part of the application, with seemingly no link with the former. At this point, good luck to analyze the reason of the bug.

    Note that autowiring by name if even worse, since bean names have a probability of collision, even across different type hierarchies.

    Autowiring is bad in Spring, but it is even worse in CDI where every class in the classpath is a candidate for injection. By the way, any CDI guru reading this post care to explain why autowiring is the only way of DI? That would be really enlightening.


    In this post, I tried to explain what is autowiring. It can be used all right, but now you should be aware of its con. IMHO, you should only use it for prototyping or quick-and-dirty proof of concepts, everything that can be discarded after a single use. If really needed, prefer wiring by type over wiring by name as at least it matching doesn’t depend on a String.

    Categories: Java Tags: autowiringCDIspring
  • CDI worse than Spring for autowiring?

    Let’s face it, there are two kinds of developers: those that favor Spring autowiring because it alleviates them from writing XML (even though you can do autowiring with XML) and those that see autowiring as something risky.

    I must admit I’m of the second brand. In fact, I’d rather face a rabbied 800-pounds gorilla than use autowiring. Sure, it does all the job for you, doesn’t it? Maybe, but it’s a helluva job and I’d rather dirty my hands than let some cheap bot do it for me. The root of the problem lies in the implicit-ness of autowiring. You declare two beans, say one needs a kind of the other and off we go.

    It seems simple on the paper and it is if we let it at that. Now, autowiring comes into two major flavors:

    • By name where matching is done between the property’s name and the bean’s name
    • By type where matching is done between the property’s type and the bean’s type

    The former, although relatively benign, can lead to naming nightmares where developers are to tweak names to make them autowire together. The second is an utter non-sense: in this case, you can create problems in a working context by creating a bean in it, only because it has the same class as another bean already existing in the context. Worse, autowiring errors can occur in a completely unrelated location, just because of the magic involved by autowiring. And no, solutions don’t come from mixing autowiring and explicit wiring, mixing autowiring between name and type or even excluding beans from being candidates for autowiring; that just worsens the complexity as developers have to constantly question what will be the behavior.

    Autowiring fans that are not convinced should read the Spring documentation itself for a list of limitations and disadvantages. This is not to say that autowiring in itself is bad, just that is has to be kept strictly in check. So far, I’ve allowed it only for small teams and only for tests (i.e. code that doesn’t ship to production).

    All in all, Spring autowiring has one redeeming quality: candidates are only chosen from the context, meaning instances outside the context cannot wreak havoc our nicely crafted application.

    CDI developers should have an hint where I’m heading. Since in CDI every class on the classpath is candidate for autowiring, this means that adding a new JAR on the application’s classpath can disrupt CDI and prevent the application from launching. In this light, only autowiring by name should be used for CDI…​ and then, only by those courageous to take the risk 🙂

    Categories: Java Tags: autowiringCDIspring
  • Managing unmanaged beans in CDI

    During these (much deserved) vacations, I worked on a pet project of mine which uses CDI with the Weld implementation and SLF4J with the Logback implementation.

    The terms of the problem were very simple:  Iwanted the logs of my application to be displayed in a Swing table, i.e. a Logback appender had to write in a table. The table was managed in CDI but the appender was not: this was no surprise since many older-generation frameworks have a lifecycle management on their own. Prior-to-JEE6 Servlets, Log4J appenders, Struts actions all have their own lifecycle management, completely unrelated to CDI. Worse, dependency injection frameworks are in large part incompatible with one another (Spring comes to mind) and need an adapter to bridge between their contexts, so this use-case is a common one.

    My first lead was to bootstrap Logback myself but since Weld uses SLF4J under the cover, I found no way. After some time, the conclusion was that CDI provided no means to inject my managed table into the Logback appender. That was a sad conclusion, but I didn’t stop there: when the specification doesn’t address your needs, jump to the implementation (but with remorse…​).

    There’s a nice complementary module to Weld called Weld Extensions, that provide additional features not mentioned in the JSR. Note that Weld Extensions seems to have been replaced by Seam Solder, but the logic stays the same: obtain a reference to the BeanManager and forcibly inject the table in the appender through it.

    In Extension, the callback is made through the Extension marker interface. Such extensions are looked up by Weld using the service provider mechanism. If you’re not familiar with it, have a look at it, it’s elegant and useful in many use-cases. So, I created my META-INF/services/javax.enterprise.inject.spi.Extension file.

    Now, let’s have a look at the extension itself:

    import java.util.List;
    import javax.annotation.PostConstruct;
    import javax.enterprise.context.spi.CreationalContext;
    import javax.enterprise.event.Observes;
    import javax.enterprise.inject.spi.AnnotatedType;
    import javax.enterprise.inject.spi.BeanManager;
    import javax.enterprise.inject.spi.Extension;
    import javax.enterprise.inject.spi.InjectionTarget;
    import org.slf4j.impl.StaticLoggerBinder;
    import ch.qos.logback.classic.Logger;
    import ch.qos.logback.classic.LoggerContext;
    import ch.qos.logback.classic.spi.ILoggingEvent;
    import ch.qos.logback.core.Appender;
    public class LogbackWeldExtension implements Extension { (1)
      public void bind(@Observes ContainerInitialized event, BeanManager manager) { (2)
        LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();
        List loggers = loggerContext.getLoggerList();
        for (Logger logger : loggers) {
          Appender appender = logger.getAppender("Table"); (3)
          if (appender != null) {
            AnnotatedType type = manager.createAnnotatedType(appender.getClass());
            InjectionTarget target = manager.createInjectionTarget(type);
            CreationalContext creationalContext = manager.createCreationalContext(null); (4)
            target.inject(appender, creationalContext);
    1 we inherit from Extension
    2 we observe the container initialized event, meaning the method is called when the Weld context is initialized. Note the BeanManager is the second parameter and is injected automatically by the framework!
    3 we use Logback’s API to get the handle on the unmanaged appender. Note that we get the reference by name, which I did because I was lazy at the time…​ The future version will browse through all appenders and get the correct one by class name.
    4 we use Weld’s API to inject the unmanaged logger into the beans that needs it. Note there’s no reference to the injected table bean, it’s just standard DI if the managed beans are correctly annotated. If you use this code, also note the compiler will warn you about the unused generics (those are a pain to work with!).

    That’s it!

    Categories: Java Tags: CDIWeld
  • TestNG, FEST et CDI

    No, those are not ingredients for a new fruit salad recipe. These are just the components I used in one of my pet project: it’ss a Swing application in which I wanted to try out CDI. I ended up with Weld SE, which is the CDI RI from JBoss.

    The application was tested alright with TestNG (regular users know about my preference of TestNG over JUnit) save the Swing GUI. A little browsing on the Net convinced me the FEST Swing testing framework was the right solution:

    • It offers a DSL for end-to-end functional testing from GUI.
    • It has an utility class that checks that Swing components methods are called on the Event Dispatch Thread (EDT).
    • It may check calls to System.exit().
    • It has a bunch of verify methods such as requireEnabled(), requireVisible(), requireValue() and many others that depend on the component’s type.

    The challenge was to make TestNG, FEST and CDI work together. Luckily, FEST already integrates TestNG in the form of the FestSwingTestngTestCase class. This utility class checks for point 2 above (EDT use rule) and create a "robot" that can simulates events on the GUI.


    FEST manages GUI interaction through fixtures, wrapper around components that can pilot tests. So, just declare your fixture as a test class attribute that will be set in the setup sequence.

    Launch Weld in tests

    FEST offers an initialization hook in the form of the onSetup() method, called by FestSwingTestngTestCase. In order to launch Weld at test setup, use the following implementation:

    protected void onSetUp() {
    	// container is of type WeldContainer
    	// it should be declared as a class attribute in order to be cleanly shutdow in the tear down step
    	container = new Weld().initialize();
    	MainFrame frame = GuiActionRunner.execute(new GuiQuery<MainFrame>() {
    		protected MainFrame executeInEDT() throws Throwable {
    			return container.instance().select(MainFrame.class).get();
    	// window is a test class attribute
    	window = new FrameFixture(robot(), frame);;

    This will display the window fixture that will wrap the application’s main window.

    Generate screenshots on failure

    For GUI testing, test failure messages are not enough. Fortunately, FEST let us generate screenshots when a test fails. Just annotate the test class:

    public abstract class MainFrameTestCase extends FestSwingTestngTestCase {

    Best practices

    Clicking a button on the frame fixture is just a matter of calling the click() method on the fixture, passing the button label as a parameter. During developement, however, I realized it would be better to create a method for each button so that it’s easier for developers to read tests.

    Expanding this best practice can lead to functional-like testing:



    I was very wary at first of testing the CDI-wired GUI. I thought it would be hard and would be too time-consuming given the expected benefits, I was wrong. Uniting TestNG and CDI is a breeze thanks to FEST. Having written a bunch of tests, I uncovered some nasty bugs. Life is good!

    Categories: Java Tags: CDIfesttestng
  • Why CDI won't replace Spring

    CDI is part of JavaEE 6 and that’s a great move forward. Now, there’s a standard telling vendors and developers how to do DI. It can be refined, but it’s here nonetheless. Norms and standards are IMHO a good thing in any industry. Yet, I don’t subscribe to some people’s points of view that this means the end of Spring. There are multiple DI frameworks around here and Spring is number one.

    Why is that? Because it was the first? It wasn’t (look at Avalon). My opinion is that it’s because Spring’s DI mechanism is only a part of its features. Sure, it’s great but Guice and now CDI offers the same. What really sets Spring apart is its integration of JavaEE API and of the best components available on the market that are built on top of DI.

    A good example of this added value is JMS: if you ever tried to post to a queue before, you know what I mean. This is the kind of code you would need to write:

    Context context = new InitialContext();
    QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context.lookup("myQConnectionFactory");
    Queue queue = (Queue) context.lookup("myQueue");
    QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
    QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    QueueSender queueSender = queueSession.createSender(queue);
    TextMessage message = queueSession.createTextMessage();
    message.setText("Hello world!");

    Now in Spring, this is configured like so:

    <jee:jndi-lookup id="queueConnectionFactory" jndi-name="myQConnectionFactory" />
    <jee:jndi-lookup id="q" jndi-name="myQueue" />
    <bean id="jmsTemplate">
      <property name="connectionFactory" ref="jmsQueueConnectionFactory" />
      <property name="defaultDestination" ref="q" />

    I don’t care it’s shorter event though it is (but it lacks the code to send the text). I don’t care it’s XML either. My real interest is that I code less: less errors, less bugs, less code to test. I don’t have to write the boilerplate code because it’s taken care of by Spring. Sure, I have to configure, but it’s a breeze compared to the code.

    This is true for JMS, but equally for Hibernate, EclipseLink, iBatis, JPA, JDO, Quartz, Groovy, JRuby, JUnit, TestNG, JasperReports, Velocity, FreeMarker, JSF, what have you that is the best of breed (or at least on top) of its category. Morevover,  if nothing exists to fit the bill, Spring provides it: look at Spring Batch for a very good example of a framework that handles the boring and repetitive code and let you focus on your business code.

    Do Guice or other frameworks have such integration features included in them? No, they are pure, "untainted" DI frameworks. As such, they can easily be replaced by CDI, Spring not so much.

    Categories: JavaEE Tags: CDIspring
  • Further into CDI

    In one of my latest articles on CDI, one of the lessons I learned was to use factory methods in order to reduce the number of classes. In this article, I will go into the detail since I think it is of some importance. More basic informations on CDI can be found in some previous posts: overview part 1 and overview part 2.

    The naive approach

    During school, you were probably taught to create a class for each component. For example, if your application has 4 buttons, North, East, South and West, you’ll have 4 classes. Each class configures its label, its icon and its action, either in the constructor or in some other method.

    In CDI, your can annotate such method with @PostConstruct in order for the container to call it just after the constructor.

    This produces the following code, when taking Swing as an example:

    public class NorthButton extends JButton {
      public NorthButton() {
        // Initializes label
        // Initializes icon
        // Initializes action

    Common behaviour and attributes may be factorized into one superclass, thus you end up with 5 classes.

    Using the right number of classes is very important in any object-oriented language:

    • If you use too few classes, you run the risk that each class you develop has too many responsabilities and are too big.
    • On the contrary, if you use too much classes, you’ll explode code into many places and it will be a maintenance nightmare. Moreover, in the Sun (Oracle ?) JVM, classes are loaded into the PermGen space which has a fixed size. Who has never seen the dreaded java.lang.OutOfMemoryError: PermGen space?

    IMHO, both are good reasons not to create a single class for each instancied component.

    The factory approach

    The second logical step is to create methods that produce the right instance. In our previous example, that would mean a factory class, with 4 methods to produce buttons, one for each button and probably a 5th method with common behaviour:

    public class ComponentFactory {
      public JButton createJButton() {
      JButton button = new JButton();
        // Set label
        // Set icon
        // Set action
        return button;

    Notice that you’ll still need an annotation for each single component:

    public @interface North {}

    In our case, that makes @North, @East, @South and @West. If these components are found throughout your application, fine (such as a Cancel button). If not, you still have a class for each component (annotations are transformed into classes at compile-time).

    The "generic" annotations approach

    CDI let you qualify injection not only from the annotation type but also from some (or all) their elements. We can thus greatly diminish the number of needed annotations. In order to do this, just create an annotation on this model:

    @Qualifier public @interface TypeName {
      Class value();
      String name();

    Now the factory method becomes:

    public class ComponentFactory {
      @TypeName(value = JButton.class, name = "North")
      @Produces public JButton createJButton() {
        JButton button = new JButton();
        // Set label
        // Set icon
        // Set action
        return button;

    Although this method meets our requirements regarding the number of classes (and annotations), it still has one major drawback: now, it’s the number of methods that is vastly increased. Though not nearly as great a disadvantage as a great number of classes, it makes the code less readable and thus less maintainable.

    The fully "generic" approach

    CDI let you use an injection point parameter for producer method. This parameter has much information about, guess what…​ the injection point. Such informations include:

    • the reflection object, depending on injection’s type, respectively Field, Method and Constructor for field, method parameter and constructor parameter injection
    • the required type to inject
    • annotations relative to the injection point

    The latter point is what drives what comes next. It means you can annotate your injection point, then get those annotations during producer method execution. In our case, we could have an annotation for each attribute we want to set (text, icon and action). Let’s create such an annotation:

    public @interface Text { String value(); }

    Such annotation could be used in the producer method in the following manner:

    public class ComponentFactory {
      public JButton createJButton(InjectionPoint ip) {
        JButton button = new JButton();
        Annotated annotated = ip.getAnnotated();
        if (annotated.isAnnotationPresent(Text.class)) {
          Text textAnnotation = (Text) annotated.getAnnotation(Text.class);
          String text = textAnnotation.value();
        // Set icon
        // Set action
        return button;

    Now, the only thing you have to do is annotate your button field like so:

    public class Xxx {
      private JButton northButton;

    This will get us a button with "North" as label. And nothing prevents you to do the same with icon and action.

    Morevover, with just text, you could go further and manage internationalization:

    public @interface Text {
      // Key in the properties file
      String value();
      // Path to the resource bundle
      String resourceBundle() default "path/to/resource/bundle";

    This annotation could also be reused of other components that manage text, such as labels.


    There’s no right or wrong approach in using CDI. However, the latter approach has the merit of being the most powerful. Additionally, it also let you write code relative to a component type in one single place. This is the road I’ve taken so far, and I’m very satisfied with it.

    Categories: Java Tags: CDI
  • Lessons learned from CDI in Swing

    Sinced I dived into CDI, I explored classical Java EE uses. Then, I used CDI into a pet project of mine to see how it could be used in Swing applications. This article sums up what lessons I learned from itthis far.

    This article assumes you have some familiarity with CDI; if not, please read my previous articles on CDI (CDI an overview part 1 and part 2) or read the documentation.

    In my last attempts at Swing (I’m more of a web developer), I figured it could be nice to configure most of the UI components in Spring rather than coding the parameters by hand. In doing this, I used the traditional XML configuration approach, since using annotations would couple my code to Spring JAR, something I loathe to do. With CDI, however, I can live with having my code tied to a standard API: I can change the implementation. This is akin to developing a servlet where you reference the Servlet API, then run it in any container you want.

    I coded and coded this way, looked at things made, refactored, then code some more, just like when it’s a personal project and you have no deadlines to meet. Then, I realized I could learn some lessons I could put to good use for the future. Here they are, in no particular order.

    The right JAR

    Getting all the right CDI JARs for your Java SE project can be a rather daunting task. In order to ease your pain, Weld provides you with a all-in-one JAR aptly named weld-se. Don’t argue that it’s bloated and repackaged, just use it, you will save yourself a lot of pain. For Maven users, this is the configuration:


    She’s an easy logger

    All the log frameworks I know of (JDK, Commons Logging, Log4J and SLF4J) let you declare your class logger rather easily:

    private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class);

    Anyway, most of the time, in order to use this, you either:

    • create a template in your IDE. Yet configuring templates in each of your IDE can be rather tedious.
      Note to self

      Propose a feature where such templates (and preferences) could be saved on the web and shared across all of my IDE instances

    • copy the line from somewhere then paste it where you need it. Then you change the class to be logged. To be frank, and though it does not add to my legend (quite the contrary) this is what I do all the time

    And doing the latter all the time, it happens I forget to change the logged class. Stupid, isn’t it? Perhaps, but what’s even more stupid is that I have to write it in the first place: the class is my context, it should be inferred. Guess what, Weld does it, with a little help from the weld-logger JAR (it uses SLF4J so beware or use the code as a template to use another framework):

    private Logger logger;

    I truly don’t think it could be easier! The only drawback is that you lose the constant: for the projects I handle now, I can live with it. To use this feature, just add the following dependency to your POM:


    Remember initialization

    CDI can of course inject your dependencies. But injecting your dependencies does not assemble them on the screen. Luckily, CDI also provides you with a hook in order to do it: this is the realm of the @PostConstruct annotation. Let’s take a very simple example:

    public class MyPanel extend JPanel {
      private JCheckBox checkbox;
      public void afterPropertiesSet() {
        setLayout(new FlowLayout());

    The afterPropertiesSet() method is called after having injected all dependencies, and plays the role previously done in the constructor.

    Yes, the method is named after an old Spring method defined in the InitializingBean interface, dating from version 1. It can now be replaced by specifying the init-method attribute of the bean in the XML beans definition file or using the @PostConstruct annotation.

    (Don’t) grow in numbers

    With CDI, your classes are injected into one another. However, this means that each component should have its own class. Tthis can be rather cumbersome: for buttons, for example, each should be attached its action on a specific basis. With time, even a moderately size application will grow in term of classs to be much bigger than a standard application. IMHO, this is not desirable since:

    • it makes your codebase bigger, and thus increase your development complexity
    • in Sun’s JVM, loading more classes mean you will use more PermGen space and you’re more likely to run into OutOfMemoryError

    In order to limit the number of classes I produce, I used a component factory filled with producer methods. These methods are identified by CDI as injection providers.

    public class ComponentFactory {
      public JButton getButton() {
        return new JButton();

    Use the injection point

    This is good but not enough, since action (or text and icon) association will have to be done in the injected class. I would like to annotate the injected attribute with informations like the text of the button and get that information in my producer method. This is the goal of the InjectionPoint optional parameter. CDI provides it to you free of charge if you reference it in your method as a parameter.

    public class ComponentFactory {
      public JButton getButton(InjectionPoint ip) {
        JButton button = new JButton();
        // Do something based on annotations on the parameter
        return button;

    This is exactly the way that Weld loggers (see above) are created.

    Respect the Higlander rule

    The Highlander rule is: "There can be only one". Apart from being taken from a movie that young people mostly don’t know, it also enunciate a basic truth. Since injection must be deterministic, there cannot be two candidates for an injection point. Using producer methods as previously stated will run you into this problem: CDI will have both the JButton class and the producer method and will loudly complains about it in its usual way.

    Exception in thread "main" org.jboss.weld.exceptions.DeploymentException:
     WELD-001409 Injection point has ambiguous dependencies.....
     at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(
     at org.jboss.weld.bootstrap.Validator.validateBean(
     at org.jboss.weld.bootstrap.Validator.validateRIBean(
     at org.jboss.weld.bootstrap.Validator.validateBeans(
     at org.jboss.weld.bootstrap.Validator.validateDeployment(
     at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(

    To be compliant with the Highlander rule, you’ll have to discard the JButton class as a candidate for injection. In order to do this, I used qualifiers, both on the injection point and on the producer method. Since I did not want a qualifier per injection point / producer method pair, I made it parameterizable:

    @Target( { FIELD, METHOD })
    public @interface JType {
      public Class<? extends JComponent> value();

    Annotating both the injected attribute and the producer method with the JType annotation made my code compliant with the Highlander rule!

    Spread the word

    A good practice I can recommend is to create a special Properties class tasked with initializing your labels (and another for your preferences, etc.), then inject it in all the client classes you need. Now, all classes have access to your internationalized labels. Truly terrific!

    Forget Java WebStart

    Weld JAR analyze process is incompatible with Java WebStart. You will likely have such nice error messages:

    122 [javawsApplicationMain] WARN - could not read entries
      http:\xxxxxxxxxx\weld-logger-1.0.0-CR2.jar (La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte)
      at Method)
      at<init>(Unknown Source)
      at<init>(Unknown Source)
      at Source)
      at Source)

    I hope it will be fixed in future releases…​

    Singletons are NOT evil

    Twice already I stumbled upon a strange behaviour: toggle buttons selected when they shouldn’t or state was lost. After debugging like mad, I saw that @PostConstruct methods where called well beyond the initial call. It seems my code called for another injection after that. In order to remedy to this, annotate your class with @Singleton in order to share the instance across multiple calls. I haven’t investigated more than that because:

    • I resolved the bug
    • I don’t know why I shouldn’t use singletons in a Swing application


    I’m still in development, so I don’t think I’ve seen all there’s to see. Yet, the previous points can make a good starting point for any project wanting to use CDI in a Java SE context.

    And please pardon the puns, I was feeling jolly because of this fine summer of ours 😅️

    Categories: Java Tags: CDISwing
  • CDI, an overview - Part 2

    In the previous part of CDI, we saw some injection, qualifiers and scope. Now, it’s time to browse through more advanced features.


    Previous examples cannot resolve all our use-cases. Some of these include:

    • injection of random values
    • injection of context-dependent value
    • in general, places where the injection process cannot be narrowed down to a simple new()

    These hint at a very well-known pattern, the factory. Factories are implemented in JSR-299 as producers.

    Let’s take a simple example, the injection of a connection from a data source. The code that gets the connection either creates it with a direct connection to the database or retrieves it from a data source pool. In the latest case, the following code would fit:

    public @interface FromDataSource {}
    public class ConnectionProducer {
      @Produces @FromDataSource
      public Connection getConnection() throws Exception {
        Context ctx = new InitialContext();
        // Read the data source name from web.xml
        String name = ...
        DataSource ds = (DataSource) ctx.lookup(name);
        return ds.getConnection();


    With Java EE 6, you can harness the power of AOP without AOP. Like in the previous example, using interceptors is very straightforward. There are 3 steps. Let’s implement a simple timer, for benchmarking purposes.

    The first step is the declaration of the interceptor. To do so, just use the @InterceptorBinding:

    @Target({METHOD, TYPE})
    public @interface Benchmarkable{}

    The second step is the interceptor implementation. It uses the `@Interceptor annotation, coupled with the previously defined one:

    @Benchmarkable @Interceptor
    public class BenchmarkInterceptor {
      public Object logPerformance(InvocationContext ic) throws Exception {
        long start = System.currentTimeMillis();
        Object value = ic.proceed();
        System.out.println(System.currentTimeMillis() - start);
        return value;


    • the method annotated with @AroundInvoke returns an Object
    • it uses a parameter of type InvocationContext

    The last step is to declare such interceptors in `WEB-INF/beans.xml because interceptors are deactivated by default.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""

    The beans.xml also tells the container about how to order the interceptors in case they is more than one.

    There are two other interceptor types, @PostConstruct and @AroundTimeout (for EJB).


    Decorators, guess what, implement the Decorator design pattern. They are very similar to interceptors with two interesting differences:

    • a decorator must implement the interface it is decorating (and yet can be abstract, so it does not have to implement the methods)
    • a decorator can have a reference to the object it decorates. It is done through injection

    Like interceptors, they must be referenced in the beans.xml file in order to be activated. Let’s take a simple example and create an interface which contract is to return an HTML representation of an object:

    public interface Htmlable {
     String toHtml();

    Now I need a date class that knows its HTML representation. I know the design is quite bad but bear with me.

    public class HtmlDate extends Date implements Htmlable {
      public String toHtml() {
        return toString();

    If I want a decorator that puts the HTML inside <strong> tags, here’s the way:

    public class StrongDecorator implements Htmlable {
      @Inject @Delegate @Any
      private Htmlable html;
      public String toHtml() {
        return "<strong>" + html.toHtml() + "</strong>";


    CDI also implements the Observer design pattern, thus at last enabling simple event-driven development paradigm on the Java EE platform. The basis for it is the event type. An event type is a simple POJO.

    The Observer is also a POJO: in order for a method of the Observer to be called when an event is fired, just add a parameter of the right event type and annotate it with @Observes:

    public class EventObserverService {
      public void afterPostEvent(@Observes PostEvent event) {
        ... // Do what must be done

    On the other side, the event producer should have an attribute of type javax.enterprise.Event parameterized with the same event type. In order to fire the event, call event.fireEvent() with an event instance:

    public class WeldServlet extends HttpServlet {
      private Event<PostEvent> event;
      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PostEvent());

    Now, when sending a POST request to the servlet, the afterPostEvent() method of the EventObserverService will be called.


    In the previous article, I adressed the mock service with calling the setter and passing a newly created instance "by hand". This is all fine and well in a unit testing case, but I also want to manage integration testing. The situation is thus the following:

    • there are two implementations of the same interface on the classpath
    • you can’t change the servlet code (for example, add a qualifier to the service attribute)

    Given the deterministic nature of CDI, you should basically be toast. In fact, nothing could be further from the truth. Just use the @Alternative annotation and CDI will conveniently ignore the annotated class.

    @Report(MAIL) @Alternative
    public class MockMailReportServiceImpl implements ReportService {

    What’s the point then to create it in the first place? Remember the unused-till-then beans.xml from above. It will come to our help, since it accepts <alternative> tags. These tags activate the alternatives.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""

    As such, you could have two beans.xml:

    • a basically empty standard context
    • and another integration testing context full of alternatives


    Seam logo

    GlassFish v3 logo

    This article was written with GlassFish v3, which uses Weld v1.0.1, as a platform. Weld is CDI reference implementation, and also a part of the Seam framework.

    I had no problems using the platform overall, yet, I couldn’t make alternatives, interceptors and decorators work. Strangely enough, all three must be configured in the WEB-INF/beans.xml. I do not know if I did something wrong or if there’s a bug in the current implementation though.


    This 2-parts article only brushes at the surface of CDI. Nevertheless, IMHO, it looks very promising and I wish it much success.

    To go further:

    • CDI, an overview - part 1
    • Commons annotations (JSR-250) page: Commons annotations has annotation for DI in Java (@Resource)
    • CDI (JSR-299) page: amazingly enough, CDI is about DI in Java EE
    • Weld’s documentation: Weld is CDI JBoss implementation and also the reference implementation
    • Article on the merits of JSR-299 compared to the merits of JSR-330
    Categories: JavaEE Tags: CDI
  • CDI, an overview - Part 1


    I may be a Spring fanboy but I’m also convinced a technology should embrace standards. Although Spring is now a de facto standard, it is in competition with other products, like Google Guice for example. It makes my work as an architect harder since my craft is to devise the most perennial solutions: standards are my allies in achieving this goal.

    CDI, formerly known as JSR 299, is an attempt at describing a true standard on Dependency Injection. What makes CDI appealing at first glance is that both SpringSource and Google took a seat in the specifications team. CDI is a part of the Java EE 6 stack, meaning an application running in a Java EE 6 compatible container can leverage CDI out-of-the-box.

    Thus, for a Maven application, all that is required is to add the following dependency:


    The basics

    How is it done? Let’s take a simple example. I still want to use a lighweight service layer: I just need a reference to such a service in my servlet. In order to do that, just add a reference to the service as an attribute and annotate it with @Inject:

    public class WeldServlet extends HttpServlet {
      @Inject private HelloService helloService;

    Note for Guice users: notice this is the same annotation, only in a standard package (javax.enterprise).

    That’s all! No fancy XML configuration, no JNDI resource to use, no nothing. The only thing to do is to follow what I call the Highlander rule:

    There can be only one.

    This rule enforces that there must be one and only one class on the classpath that extends HelloService, thus fulfilling the interface contract. This is common sense: the injection must be deterministic and you can’t if you have more than one implementation to choose from.


    In truth, if you just use the @Inject annotation, you probably will be faced by a NullPointerException. For CDI to activate, you’ll need to have a XML configuration file named beans.xml under WEB-INF for web applications or META-INF for jars. This file can be empty but is mandatory in order for CDI to bootstrap.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""


    Yet, such condition is not met most of the time since you’ll probably have at least another mock implementation for your unit tests. A concept is missing somewhere: it is the qualifier. Qualifiers add a quality to injection so that the Highlander rule is enforced between all classes that meet all the injection qualifiers. Thus, one can add a distinguishing qualifier to the mock service to resolve our quandary. Let’s design such a qualifier:

    • to be considered a qualifier, it uses the @Qualifier annotation
    • since injection is done at runtime, the retention policy must be runtime
    • target will be type, since it’s the class itself that will be annotated

    The result is the following:

    @Target( { TYPE })
    public @interface Mock {}

    Just annotate your mock service with @Mock and presto, the injection will succeed again. We haven’t seen how to inject the mocked service, but please bear with me, it will be adressed later.

    Setter injection

    In fact, this situation should not really happen (at least if you use Maven), since your standard classpath and your testing classpath should be different. Moreover, your unit testing should IMHO not use injection. This would slightly change my servlet to be used both in standard context and unit testing context: I will need to be able to inject in the former case and to set manually in the latter. This is not a problem, since CDI also accepts setter injection like in the following snippet:

    public class WeldServlet extends HttpServlet {
      private HelloService helloService;
      public void setHelloService(HelloService helloService) {
        this.helloService = helloService;

    More qualifiers

    As we saw, the qualifier use-case from above was not a good example. A much better one would be the need for a servlet to report an error. There are much ways of reporting this: mail, log, SMS, etc. The service used to report would be dependent of the servlet, meaning all the services should be available on the classpath. Now, as we have seen previously with @Mock, each service would be annotated with @Mail, @Log, @SMS, etc. What we did not see is how to inject the right service. Nothing could be easier, just tell CDI that which service you need by providing the needed qualifier:

    public class WeldServlet extends HttpServlet {
      @Inject @Mail private ReportService reportService;

    When you do not define any qualifier, CDI will use one for you under the cover, @Default. That’s why just annotating the mock service with @Mock succeeded: the real service was annotated implicitly with @Default and that was enough to fulfill the Higlander rule.

    Qualifiers with attributes

    Using the previous method will likely lead to an exponential proliferation of qualifiers that contradict Java EE 6 goals of readibility and maintainability. CDI will still let you reach them goals with annotation members. Now, the classes are:

    public enum ReportType {
      MAIL, SMS, LOG;    // Adding one more type here is easy
    @Target( { TYPE, FIELD })
    public @interface Report {
        ReportType value();
    public class MailReportServiceImpl implements ReportService {...}
    public class WeldServlet extends HttpServlet {
      @Inject @Report(MAIL)
      private ReportService reportService;

    Creating another reporting service consists of creating the service implementation itself and adding a value to the enumeration.


    Traditionally, services are stateless and, as such, have little interest in being instantiated more than once: it’s a good practice to make them singletons. Frameworks such as Spring make container-managed beans singletons as a default. Singletons creation is a feature of CDI but beware that singletons should be explicitly marked as such:

    public class HelloServiceImpl implements HelloService {

    Scoped beans

    Singletons are only a particular case of a scope. In Java EE, scopes are well-defined regarding web applications: application, session, request and page. CDI does not manage page scope and adds a conversation scope that is tied to JSF. Scope usage in CDI is similar as Spring: injected bean will be tied to the defined scope and its visibility restricted to that scope.

    Information relative to settings and preferences could well take place in a session-scoped bean for instance. Just add the right scope annotation to this bean:

    public class UserSettings implements Serializable {
      private static final long serialVersionUID = 1L;

    At this point, you should probably able to adress 80% of your needs. The next article will tackle advanced subjects like producers and alternatives.

    Categories: JavaEE Tags: CDI