So far this series has covered simple IoC dependency injection as well as bean initialization with @PostConstruct and constructor injection.

In this part we will introduce container managed contextual scopes to Voodoo DI.

A scope defines the life-cycle of a bean, allowing state to be preserved and or shared across multiple interactions. DI frameworks offer a fast variety of build-in and add-on scopes, such as @RequestScoped, @SessionScoped, @ApplicationScoped, etc…

So far Voodoo DI simply maps interfaces, supertypes and the target type directly to the concrete implementations.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 //Map Interfaces and Supertypes to concrete implementation. private final Map < Class , Class > types = new ConcurrentHashMap <> ( ) ; . . . public <T> T instance ( Class <T> clazz ) { T newInstance = null ; try { Constructor <T> constructor = types . get ( clazz ) . getConstructor ( new Class [ ] { } ) ; newInstance = constructor . newInstance ( new Object [ ] { } ) ; processFields ( clazz , newInstance ) ; } catch ( Exception ex ) { Logger . getLogger ( Voodoo . class . getName ( ) ) . log ( Level . SEVERE , null , ex ) ; throw new RuntimeException ( ex ) ; } return newInstance ; } . . .



For the container to manage contextual instances, the concrete implementation has to be wrapped into a contextual type.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 public abstract class ContextualType <T> { private final Class <T> type ; public ContextualType ( Class <T> type ) { this . type = type ; } public abstract T getContextualInstance ( ) ; public Class <T> getType ( ) { return type ; } }

ContextualType.class V3

The actual life-cycle management is then performed in the context specific ContextualType implementation.

In the simplest case, similar to Spring’s prototype and CDI dependent scope, Voodoo default “Puppet” scope will return a new instance every time.

1 2 3 4 5 6 7 8 9 10 11 12 13 public class PuppetContextualType <T> extends ContextualType { . . . @Override public T getContextualInstance ( ) { try { Constructor <T> constructor = getType ( ) . getConstructor ( new Class [ ] { } ) ; return constructor . newInstance ( new Object [ ] { } ) ; } catch ( Exception ex ) { Logger . getLogger ( PuppetContextualType . class . getName ( ) ) . log ( Level . SEVERE , null , ex ) ; throw new RuntimeException ( ex ) ; } } }

PuppetContextualType.class V3

Instead of directly mapping the types to their concrete implementations, the concrete implementations have to be wrapped into the PuppetContextualType.

1 2 3 4 5 6 7 8 9 10 11 private void scan ( String packageName ) { List <Class> discoveredTypes = TypeScanner . find ( packageName ) ; discoveredTypes . stream ( ) . filter ( ( type ) -> ( ! type . isInterface ( ) && ! Modifier . isAbstract ( type . getModifiers ( ) ) ) ) . forEach ( ( Class type ) -> { ContextualType contextualType = new PuppetContextualType ( type ) ; types . put ( type , contextualType ) ; registerInterfaces ( contextualType ) ; registerSuperTypes ( contextualType ) ; } ) ; }

VoodooDI.class V3

Voodoo DI now obtains the ContextualType from the types map. The actual instantiation and life-cycle management of the targeted instance is now performed by the ContextualType implementation.

1 2 3 4 5 6 7 8 9 10 11 12 private final Map < Class , ContextualType > types = new ConcurrentHashMap <> ( ) ; . . . public <T> T instance ( Class clazz ) { T newInstance = null ; try { newInstance = ( T ) types . get ( clazz ) . getContextualInstance ( ) ; processFields ( clazz , newInstance ) ; } catch ( Exception ex ) { Logger . getLogger ( Voodoo . class . getName ( ) ) . log ( Level . SEVERE , null , ex ) ; } return newInstance ; }

Singleton

Introducing Singleton Scope to Voodoo DI is a simple matter of implementing a singleton ContextualType.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class SingletonContextualType <T> extends ContextualType { private final ReentrantLock reentrantLock = new ReentrantLock ( ) ; private T singleton ; public SingletonContextualType ( Class type ) { super ( type ) ; } @Override public T getContextualInstance ( ) { if ( singleton == null ) { initalizeSingleton ( ) ; } return singleton ; } private void initalizeSingleton ( ) { reentrantLock . lock ( ) ; if ( singleton == null ) { singleton = ( T ) createInstance ( getType ( ) ) ; } reentrantLock . unlock ( ) ; } }

SingletonContextualType.class V4

JSR-330 provides a standardized @Singleton annotation with which types can be marked to be managed by the singleton context.

1 2 3 4 @Singleton public class Highlander { . . . }

During initialization all identified types are checked for @Singleton annotation, if present the type will be wrapped into a SingletonContextualType instead of the default PuppetContextualType.

1 2 3 4 5 6 7 8 9 10 . . . private ContextualType buildContextualInstance ( Class type ) { Annotation singleton = type . getAnnotation ( Singleton . class ) ; if ( singleton == null ) { return new PuppetContextualType ( type ) ; } else { return new SingletonContextualType ( type ) ; } } . . .

Voodoo.class V4

Custom Scopes

So far basic context have been introduced to Voodoo DI, however these are implemented in a rather static non-extensible fashion (if(context) {...} else {...}).

To allow for custom contexts to be added we will have to enable Voodoo DI to discover available contexts on the classpath.

1 2 3 4 5 6 7 8 9 10 public abstract class Context { public Set < ? extends ContextualType > initalizeContextualTypes ( Set <Class> types ) { //Find class with ContextAnnotation and build contextual instances. . . . } //Context Annotation used for contextual type scanning. public abstract Class getContextAnnotation ( ) ; protected abstract ContextualType buildContextualType ( Class type ) ; }

For this all available contexts have to extend Context.

So far we have relied on Voodoo DI TypeScanner, as stated previously this was solely implemented to proof that its possible. From here on Voodoo DI will utilize on Ron Mamo reflections library for type scanning.

To start with, Reflections scans the entire classpath, just as was the case with the TypeScanner. With reflections.getSubTypesOf(Context.class) we then discover all available contexts.

1 2 3 4 5 6 7 8 . . . public static Map < Class , ContextualType > process ( String packageName ) { //Scan entire classpath Reflections reflections = new Reflections ( new ConfigurationBuilder ( ) . addUrls ( ClasspathHelper . forManifest ( ) ) ) ; Set < Class < ? extends Context >> contexts = reflections . getSubTypesOf ( Context . class ) ; . . . } . . .

ContextManager.class V5

Each discovered context is constructed(5) and initializes all its contextual types(6).

1 2 3 4 5 6 7 8 9 10 11 . . . public static Map < Class , ContextualType > process ( String packageName ) { . . . Stream <ContextualType> contextualTypes = contexts . parallelStream ( ) . map ( contextType -> constructContext ( contextType ) ) . map ( context -> initalizeContextualTypes ( context , reflections ) ) . flatMap ( ctypes -> ctypes . stream ( ) ) ; return mapTypes ( contextualTypes ) ; } . . .

ContextManager.class V5

The contextual types from the initialized contexts are merged(7) and as before mapped(9) to their respective type, supertypes and interfaces.

If we wanted to create a context that returns a random contextual instance (a.k.a. Pooled) to Voodoo DI we simply have to define an context annotation to mark the targeted beans.

1 2 3 @Scope @Inherited public @interface Random { }

Implement the Context, defining the annotation and implementing the contextual instance factory method.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class RandomContext extends Context { private static final Class SCOPE_ANNOTATION = Random . class ; @Override public Class getContextAnnotation ( ) { return SCOPE_ANNOTATION ; } @Override protected ContextualType buildContextualType ( Class type ) { return new RandomContextualType ( type ) ; } }

Last but not least we will implement the contextual logic in by implementing the RandomContextualType.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class RandomContextualType <T> extends ContextualType { private final List <T> randomInstance = new ArrayList <> ( ) ; public RandomContextualType ( Class <T> type ) { for ( int i = 0 ; i < 10 ; i ++ { randomInstance . add ( createInstance ( type ) ) ; } super ( type ) ; } @Override public T getContextualInstance ( ) { int index = new Random ( ) . nextInt ( catalogue . size ( ) ) ; return catalogue . get ( index ) ; } }

Using the new random context is a simple matter of annotating the targeted bean with @Random.

1 2 3 4 @Random public class MyRandomBean { . . . }

In this post we covered one of the most useful features of DI frameworks, managing contextual instances. In the next post of this series we will have a look at interceptors.