*/
package org.wamblee.inject;
-
-
/**
* This abstract base class performs injection at construction. Be sure not to
* initialize fields of derived classes to null as these will override the
* initializations of this base class.
*
- * This class uses {@link InjectorBuilder} to obtain an implementation of
- * a {@link InjectorFactory} to use.
+ * This class uses {@link InjectorBuilder} to obtain an implementation of a
+ * {@link InjectorFactory} to use.
*
* @author Erik Brakkee
*/
public abstract class Injectable {
- private final Injector injector =
- InjectorBuilder.getInjector();
+ private final Injector injector = InjectorBuilder.getInjector();
/**
* Inheritance style constructor.
inject();
}
-
/**
- * Inject references. This can be useful when injection is to be done again after construction
- * to obtain new references or after zeroing out of the references.
+ * Inject references. This can be useful when injection is to be done again
+ * after construction to obtain new references or after zeroing out of the
+ * references.
*/
public void inject() {
injector.inject(this);
- }
-
-
+ }
+
/**
* Gets the default injector.
- * @return Injector.
+ *
+ * @return Injector.
*/
public Injector getInjector() {
return injector;
package org.wamblee.inject;
/**
- * Interface used to perform injection with. An injector instance knows how to perform injection in
- * one or more specific classes.
+ * Interface used to perform injection with. An injector instance knows how to
+ * perform injection in one or more specific classes.
*
* @author Erik Brakkee
*/
*
* @param aComponent
* Component to inject into.
- * @throws IllegalArgumentException In case the argument passed is not supported for injection
- * @throws NullPointerException In case the argument passed is null.
+ * @throws IllegalArgumentException
+ * In case the argument passed is not supported for injection
+ * @throws NullPointerException
+ * In case the argument passed is null.
*/
void inject(Object aComponent);
/**
* Utility for obtaining an implementation of the {@link InjectorFactory} using
- * {@link ServiceLoader} and for obtaining a {@link SimpleInjector}.
+ * {@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.
+ * 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 InjectorBuilder {
private static InjectorFactory FACTORY;
-
- private static SimpleInjector INJECTOR;
+
+ private static SimpleInjector INJECTOR;
/**
- * Sets the injector factory. This is useful for testing.
- * @param aFactory Factory to use.
+ * Sets the injector factory. This is useful for testing.
+ *
+ * @param aFactory
+ * Factory to use.
*/
public static void setInjectorFactory(InjectorFactory aFactory) {
FACTORY = aFactory;
*/
public static InjectorFactory getInjectorFactory() {
if (FACTORY == null) {
- FACTORY = findInjectorFactory();
+ FACTORY = findInjectorFactory();
INJECTOR = new SimpleInjector(FACTORY);
}
return FACTORY;
}
-
+
/**
- * Gets an injector that support injection into any type of object and
- * performs caching of the injector obtained from the {@link InjectorFactory}.
+ * Gets an injector that support injection into any type of object and
+ * performs caching of the injector obtained from the
+ * {@link InjectorFactory}.
+ *
* @return Injector.
*/
- public static Injector getInjector() {
+ public static Injector getInjector() {
getInjectorFactory();
return INJECTOR;
}
/**
* Constructs an empty cache.
*
- * @param aInjectorFactory Injector factory to create Injectors.
+ * @param aInjectorFactory
+ * Injector factory to create Injectors.
*/
public InjectorCache(InjectorFactory aInjectorFactory) {
injectorFactory = aInjectorFactory;
*/
package org.wamblee.inject;
-
/**
- * The injector factory is responsible for creating injectors for a given class.
- * This must be implemneted to interface to specific dependency injection frameworks or for
- * testing.
+ * The injector factory is responsible for creating injectors for a given class.
+ * This must be implemneted to interface to specific dependency injection
+ * frameworks or for testing.
*
- * Implementations of this class must have a default no-arg constructor to be usable by
- * {@link InjectorBuilder}.
+ * Implementations of this class must have a default no-arg constructor to be
+ * usable by {@link InjectorBuilder}.
*
* @author Erik Brakkee
*/
/**
* Creates an injector.
+ *
* @return Injector fot he given class.
- * @throws IllegalArgumentException In case the given class cannot be used.
+ * @throws IllegalArgumentException
+ * In case the given class cannot be used.
*/
Injector create(Class aClass);
*/
package org.wamblee.inject;
-
/**
* The main entry point for programmatic dependency injection. A different
* {@link InjectorFactory} can be plugged in for testing.
* <p>
* Given the following class:
* </p>
+ *
* <pre>
* class Pojo {
* @EJB
* The <code>SimpleInjector</code> should be cached. This is because the
* <code>SimpleInjector</code> caches the {@link Injector} objects that it uses
* internally for performance. This is done because creation of these internal
- * <code>Injector</code> objects may be costly. Caching the simple injector makes sure
- * that a class is not analysed again for annotations every time injection is
- * used.
+ * <code>Injector</code> 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 InjectorBuilder}.
+ * constructed instead of being obtained through the {@link InjectorBuilder}.
*
* @author Erik Brakkee
*/
public class InjectableTest {
@Test
- public void testInjectInDerivedClass() {
- Pojo pojo = new Pojo();
- assertEquals(TestInjectorFactory.MAGIC_VALUE, pojo.getValue());
+ public void testInjectInDerivedClass() {
+ Pojo pojo = new Pojo();
+ assertEquals(TestInjectorFactory.MAGIC_VALUE, pojo.getValue());
}
}
import org.junit.After;
import org.junit.Test;
-import static junit.framework.TestCase.*;
+import static junit.framework.TestCase.*;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.*;
public class InjectorBuilderTest {
-
+
@After
- public void tearDown() {
+ public void tearDown() {
InjectorBuilder.setInjectorFactory(null);
}
@Test
- public void testGetInjectorFactory() {
- InjectorFactory factory = InjectorBuilder.getInjectorFactory();
+ public void testGetInjectorFactory() {
+ InjectorFactory factory = InjectorBuilder.getInjectorFactory();
assertTrue(factory instanceof TestInjectorFactory);
}
-
+
@Test
- public void testOVerrideInjectorFactory() {
+ public void testOVerrideInjectorFactory() {
InjectorFactory factory = mock(InjectorFactory.class);
-
- InjectorBuilder.setInjectorFactory(factory);
- InjectorFactory factory2 = InjectorBuilder.getInjectorFactory();
- assertSame(factory, factory2);
+
+ InjectorBuilder.setInjectorFactory(factory);
+ InjectorFactory factory2 = InjectorBuilder.getInjectorFactory();
+ assertSame(factory, factory2);
}
}
import org.junit.Test;
public class InjectorCacheTest {
-
- private static class MyInjector implements Injector {
- private String classname;
-
- public MyInjector(String aClassname) {
- classname = aClassname;
+
+ private static class MyInjector implements Injector {
+ private String classname;
+
+ public MyInjector(String aClassname) {
+ classname = aClassname;
}
+
@Override
public void inject(Object aComponent) {
throw new RuntimeException("not implemented");
}
+
public String getClassname() {
return classname;
}
}
@Test
- public void testCache() {
+ public void testCache() {
InjectorFactory factory = new InjectorFactory() {
-
+
@Override
public Injector create(Class aClass) {
return new MyInjector(aClass.getName());
}
};
- InjectorCache cache = new InjectorCache(factory);
+ InjectorCache cache = new InjectorCache(factory);
// Unrealistic exmaple, but sufficient for test.
-
+
Injector injector = cache.getInjector(String.class);
- assertEquals(String.class.getName(), ((MyInjector)injector).getClassname());
-
- Injector injector2 = cache.getInjector(String.class);
+ assertEquals(String.class.getName(), ((MyInjector) injector)
+ .getClassname());
+
+ Injector injector2 = cache.getInjector(String.class);
assertSame(injector, injector2);
-
- // verify we get another one for another class.
+
+ // verify we get another one for another class.
Injector injector3 = cache.getInjector(Long.class);
- assertEquals(Long.class.getName(), ((MyInjector)injector3).getClassname());
-
+ assertEquals(Long.class.getName(), ((MyInjector) injector3)
+ .getClassname());
+
}
}
public class Pojo extends Injectable {
- private String value;
-
+ private String value;
+
public void setValue(String aValue) {
value = aValue;
}
-
+
public String getValue() {
return value;
}