X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Finject%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Finject%2FSimpleInjector.java;h=9e83064c98cbe5e8f7f5007e94e0fb14ff751b82;hb=466df0c4c373fe3d86a851fa4adbf72d015a0225;hp=61bb55608b33839d8012e42b5006de2c90218b68;hpb=8dff4cb222f81b1a04221fc0fa284fa5ea14d4ed;p=utils diff --git a/support/inject/src/main/java/org/wamblee/inject/SimpleInjector.java b/support/inject/src/main/java/org/wamblee/inject/SimpleInjector.java index 61bb5560..9e83064c 100644 --- a/support/inject/src/main/java/org/wamblee/inject/SimpleInjector.java +++ b/support/inject/src/main/java/org/wamblee/inject/SimpleInjector.java @@ -15,35 +15,45 @@ */ package org.wamblee.inject; -import java.util.ServiceLoader; - /** - * Singleton injector access. This should be used as main entry point for - * injection. A different {@link InjectorFactory} can be plugged in for testing. + * The main entry point for programmatic dependency injection. A different + * {@link InjectorFactory} can be plugged in for testing. * - * Given the following class: - *
- * class Pojo { - * @EJB - * private Service service; + *+ * Given the following class: + *
+ *+ * class Pojo { + * @EJB + * private Service service; * - * ... - * } + * ... + * } *- * injecting the EJB into a POJO using Contexts and Dependency Injection is accomplished as follows: + * + * injecting the EJB into a POJO is accomplished as follows: + * *- * Pojo pojo = new Pojo(); - * SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory()); - * injector.inject(pojo); + * Pojo pojo = new Pojo(); + * SimpleInjector injector = new SimpleInjector(InjectorFactoryBuilder + * .getInjectorFactory()); + * 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. + * Of course, the above example assumes the the injector understands the + * @EJB annotation (which of course CDI does). * - * The {@link InjectorFactory} to be used can also be found using a {@link ServiceLoader} discovery - * mechanism as provided by {@link InjectorFactoryBuilder}. + * TheSimpleInjector
should be cached. This is because the + *SimpleInjector
caches the {@link 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 + * {@link InjectorFactoryBuilder}. * * @author Erik Brakkee */ @@ -52,13 +62,15 @@ public class SimpleInjector { private InjectorCache cache; /** - * Constructs the injector. - * @param aFactory Factory to use. + * Constructs the injector. + * + * @param aFactory + * Factory to use. */ - public SimpleInjector(InjectorFactory aFactory) { + public SimpleInjector(InjectorFactory aFactory) { cache = new InjectorCache(aFactory); } - + /** * Injects into a given object. *