X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=support%2Finject%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Finject%2FSimpleInjector.java;h=449a65a33162bf97114510bbafd8879e5fcce8fc;hb=31c8401aa25ebc8791bf190152efbcdb0d4e860c;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..449a65a3 100644 --- a/support/inject/src/main/java/org/wamblee/inject/SimpleInjector.java +++ b/support/inject/src/main/java/org/wamblee/inject/SimpleInjector.java @@ -15,50 +15,61 @@ */ 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: + *
* - * Given the following class: - *- * class Pojo { - * @EJB - * private Service service; + *+ * 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). + * + * 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. * - * The {@link InjectorFactory} to be used can also be found using a {@link ServiceLoader} discovery - * mechanism as provided by {@link InjectorFactoryBuilder}. + * For more advanced cases, the injector factory can also be directly + * constructed instead of being obtained through the {@link InjectorBuilder}. * * @author Erik Brakkee */ -public class SimpleInjector { +public class SimpleInjector implements Injector { 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. *