Building extensible Java EE Applications – Part 1

With the advent of Java EE 6 it has become easier to build extensible applications. In this first post we will briefly explore how core components of standard applications can be customized with CDI @Specializes/@Decorator and more interestingly packaging the customized applications.

Specializes

Specialization in Java EE allows for simple customization of existing beans via inheritance. If a @Specializes bean is found on the classpath it will be injected instead of the original.

The specializing bean has to extend the original bean, it inherits all qualifiers and its name, if defined with @Named.

@RequestScoped
public class CoreService {
    public void doStuff() {
        System.out.println("Default implementation is doing Stuff.");
    }
}

@Specializes
public class MySpezializedService extends CoreService {
    @Override
    public void doStuff() {
        System.out.println("Spezialized implementation is doing Stuff.");
    }
}

MySpezializedService will be injected instead of MyDefaultService in any place where MyDefaultService has been defined for injection.
Continue reading Building extensible Java EE Applications – Part 1