Dependency Injection in RichFaces framework.

Version 2

    Dependency Injection gives a power for services and simplifies development, so it would be nice to use such technology for RichFaces framework itsels.

    Proposed implementation:

    ServiceTracker class in the richfaces-API that provides implementation-independent entry point. That class delegates calls for service instances ( ServiceTracker.getService( Class service class ) to the implementation instance. RichFaces initialization code should choose available implementation and register instance of DependencyInjection interface that wraps particular implementation. That makes project independent from implementation.

    To simplify built-in implementation that provided if no other is available ( non-jee6 environment, like Tomcat6 ), RichFaces uses JSR-330 javax.inject annotations only. The only @Named classifier supported, and only @Singleton scope ( by default, injection service creates object instances by demand ). If necessary, we would add other scopes ( session, request, facesContext and so on ) later.

    To avoid unnecessary dependency from JSR-299 classes, we will use beans.xml to configure service implementation and use Guice-style Java configuration otherwise ( for minimal implementation). To register service instance in any project, developer has to:

    1. Create class with default constructor implemented BindingModule interface and write code to register service instances:

     

    public class FooModule implements BindingModule {
     
                public void configure(InjectorConfig injector) {
                    injector.register(FooInterface.class).to(FooImpl.class).asSingleton();
                    injector.register("foo", String.class).toInstance("bar");
                    injector.register("bar", String.class).toInstance("baz");
                }
                
            }
    
    

    Add qualified name of the class to file META-INF/services/org.richfaces.jsr330.Module , as it described for ServiceLoader class:

    #  Resource-related services implementation of List
    org.richfaces.resource.Services # comment
    foo.bar.CustomSkinsModule # overrides default Skin service
     
    

    RichFaces initialization module discovers all these modules and use them to configure services instances. For CDI environment, implementations should be defined in META-INF/beans.xml file.