org.wamblee.inject
Class SimpleInjector

java.lang.Object
  extended by org.wamblee.inject.SimpleInjector
All Implemented Interfaces:
Injector

public class SimpleInjector
extends java.lang.Object
implements Injector

The main entry point for programmatic dependency injection. A different InjectorFactory can be plugged in for testing.

Given the following class:

   class Pojo {
     @EJB
     private Service service; 
   
     ...
   }
 
injecting the EJB into a POJO is accomplished as follows:
 Pojo pojo = new Pojo();
 SimpleInjector injector = new SimpleInjector(InjectorFactoryBuilder
     .getInjectorFactory());
 injector.inject(pojo);
 
Of course, the above example assumes the the injector understands the @EJB annotation (which of course CDI does). The SimpleInjector should be cached. This is because the SimpleInjector caches the Injector objects that it uses internally for performance. This is done because creation of these internal Injector objects may be costly. Caching the simple injector makes sure that a class is not analysed again for annotations every time injection is used. For more advanced cases, the injector factory can also be directly constructed instead of being obtained through the InjectorBuilder.

Author:
Erik Brakkee

Constructor Summary
SimpleInjector(InjectorFactory aFactory)
          Constructs the injector.
 
Method Summary
 void inject(java.lang.Object aObject)
          Injects into a given object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleInjector

public SimpleInjector(InjectorFactory aFactory)
Constructs the injector.

Parameters:
aFactory - Factory to use.
Method Detail

inject

public void inject(java.lang.Object aObject)
Injects into a given object.

Specified by:
inject in interface Injector
Parameters:
aObject - Object to inject into.


Copyright © 2022. All Rights Reserved.