X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fcdi%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fcdi%2FSimpleInjector.java;h=fff8f113be13fe8525e6b850366f7fac05b42589;hb=efe818bf40c144c19d25c4df283996667de84cf4;hp=7a8db0da4397c21531acfcf3e5d75522248d6431;hpb=cbb032a4e384ae54d9c7f5f42758622f53a17bb7;p=utils diff --git a/support/cdi/src/main/java/org/wamblee/cdi/SimpleInjector.java b/support/cdi/src/main/java/org/wamblee/cdi/SimpleInjector.java index 7a8db0da..fff8f113 100644 --- a/support/cdi/src/main/java/org/wamblee/cdi/SimpleInjector.java +++ b/support/cdi/src/main/java/org/wamblee/cdi/SimpleInjector.java @@ -19,30 +19,55 @@ package org.wamblee.cdi; * Singleton injector access. This should be used as main entry point for * injection. A different {@link 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(); 
+ *   injector.inject(pojo);
+ * 
+ * + * Note that it is recommended to cache the injector because the injector does caching + * of the types that it injects into. Caching the injector makes sure that a class is not + * analysed again for annotation every time injection is used. + * * @author Erik Brakkee */ public class SimpleInjector { - private static InjectorCache cache = new InjectorCache( + private InjectorCache cache = new InjectorCache( new CdiInjectorFactory()); /** - * Override the injector factory (mainly fo runit test). - * - * @param aFactory - * Factory. + * Constructs the injector. */ - public static void setInjectorFactory(InjectorFactory aFactory) { + public SimpleInjector() { + cache = new InjectorCache(new CdiInjectorFactory()); + } + + /** + * Construcst the injector. + * @param aFactory Factory to use. + */ + public SimpleInjector(InjectorFactory aFactory) { cache = new InjectorCache(aFactory); } - + /** * Injects into a given object. * * @param aObject * Object to inject into. */ - public static void inject(Object aObject) { + public void inject(Object aObject) { cache.getInjector(aObject.getClass()).inject(aObject); } }