(no commit message)
authorErik Brakkee <erik@brakkee.org>
Thu, 22 Jul 2010 08:47:47 +0000 (08:47 +0000)
committerErik Brakkee <erik@brakkee.org>
Thu, 22 Jul 2010 08:47:47 +0000 (08:47 +0000)
support/inject/src/main/java/org/wamblee/inject/Injectable.java
support/inject/src/main/java/org/wamblee/inject/InjectorFactoryBuilder.java

index 4f55cc35e2db8cc05da4766bde0424c794629810..b44192cfa22b81de14bb215fa535f5ed9baed3eb 100644 (file)
@@ -29,13 +29,21 @@ package org.wamblee.inject;
  */
 public abstract class Injectable {
 
-    private static final SimpleInjector INJECTOR = new SimpleInjector(
-        InjectorFactoryBuilder.getInjectorFactory());
+    private final SimpleInjector injector = 
+        InjectorFactoryBuilder.getInjector();
 
     /**
      * Inheritance style constructor.
      */
     protected Injectable() {
-        INJECTOR.inject(this);
+        injector.inject(this);
     }    
+    
+    /**
+     * Gets the default injector.
+     * @return Injector. 
+     */
+    public SimpleInjector getInjector() {
+        return injector;
+    }
 }
index 13db8412d886a54474b808aa329ff69792aa7327..197311a8b262927ee39d9b45e6a9fcea79940217 100644 (file)
@@ -20,13 +20,21 @@ import java.util.ServiceLoader;
 
 /**
  * Utility for obtaining an implementation of the {@link InjectorFactory} using
- * {@link ServiceLoader}.
+ * {@link ServiceLoader} and for obtaining a {@link SimpleInjector}. 
+ * 
+ * The builder takes care that the factory and simple injector are built only once. 
+ * For test code, make sure to call {@link #setInjectorFactory(InjectorFactory)}
+ * before each test case to force the retrieval of a new factory and injector. This 
+ * is important because if the simple injector is not created again it will use 
+ * cached {@link Injector} instances from other tests. 
  * 
  * @author Erik Brakkee
  */
 public class InjectorFactoryBuilder {
 
     private static InjectorFactory FACTORY;
+    
+    private static SimpleInjector INJECTOR; 
 
     /**
      * Sets the injector factory. This is useful for testing. 
@@ -34,6 +42,7 @@ public class InjectorFactoryBuilder {
      */
     public static void setInjectorFactory(InjectorFactory aFactory) {
         FACTORY = aFactory;
+        INJECTOR = new SimpleInjector(aFactory);
     }
 
     /**
@@ -45,9 +54,15 @@ public class InjectorFactoryBuilder {
     public static InjectorFactory getInjectorFactory() {
         if (FACTORY == null) {
             FACTORY = findInjectorFactory(); 
+            INJECTOR = new SimpleInjector(FACTORY);
         }
         return FACTORY;
     }
+    
+    public static SimpleInjector getInjector() { 
+        getInjectorFactory();
+        return INJECTOR;
+    }
 
     /**
      * Finds the injector factory musing <code>ServiceLoader</code>