import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionTarget;
+import org.wamblee.inject.Injector;
+
/**
* Class encapsulating bean injection into a specific non-contextual object of a
* given class.
import javax.enterprise.inject.spi.BeanManager;
+import org.wamblee.inject.Injector;
+import org.wamblee.inject.InjectorFactory;
+
/**
* Factory that creates CDI injectors. In case no beanmanager is found then
* injectors will do nothing. This class may be subclassed for testing to
* CDI can be used to inject dependencies such as for example @EJB, @Resource, @PersistenceContext,
* and @Inject into any object.
*/
-package org.wamblee.cdi;
\ No newline at end of file
+package org.wamblee.cdi;
+import org.wamblee.inject.SimpleInjector;
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.wamblee.cdi;
+package org.wamblee.inject;
+
+import java.util.ServiceLoader;
+
/**
- * 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.
- *
- * Use this by subclassing through
- * {@link #Injectable()).
+ * 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.
+ *
+ * Use this by subclassing through {@link #Injectable()).
+ *
+ * To use this class, the {@link ServiceLoader} mechanism must be used to locate
+ * a {@link InjectorFactory}. The first implementation that is found will be
+ * used for injection.
*
* @author Erik Brakkee
*/
public abstract class Injectable {
-
- private static final SimpleInjector INJECTOR = new SimpleInjector();
+
+ private static final SimpleInjector INJECTOR = new SimpleInjector(
+ InjectorFactoryBuilder.getInjectorFactory());
/**
* Inheritance style constructor.
*/
protected Injectable() {
INJECTOR.inject(this);
- }
+ }
}
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.wamblee.cdi;
+package org.wamblee.inject;
/**
- * Interface used to perform injection with.
+ * 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.
*/
- public abstract void inject(Object aComponent);
+ void inject(Object aComponent);
}
\ No newline at end of file
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.wamblee.cdi;
+package org.wamblee.inject;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import org.wamblee.cdi.CdiInjector;
+
/**
* Cache of {@link CdiInjector}s for efficiency to avoid duplicate analysis of a
* given class.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.wamblee.cdi;
+package org.wamblee.inject;
+
/**
- * Injector factory used. This may be subclasses for testing.
+ * Injector factory used. This creates an injector that is appropriate for a certain class.
+ * May be subclassed for testing or other advanced usage (even replacing CDI with another
+ * injection framework).
+ *
+ * Implementations of this calss must have a default no-arg constructor.
*
* @author Erik Brakkee
*/
public interface InjectorFactory {
+ /**
+ * Creates an injector.
+ * @return Injector fot he given class.
+ * @throws IllegalArgumentException In case the given class cannot be used.
+ */
Injector create(Class aClass);
}
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.wamblee.cdi;
+package org.wamblee.inject;
+
+import java.util.ServiceLoader;
+
/**
* Singleton injector access. This should be used as main entry point for
* ...
* }
* </pre>
- * injecting the EJB into a POJO is accomplished as follows:
+ * injecting the EJB into a POJO using Contexts and Dependency Injection is accomplished as follows:
* <pre>
* Pojo pojo = new Pojo();
- * SimpleInjector injector = new SimpleInjector();
+ * SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory());
* injector.inject(pojo);
* </pre>
*
* 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.
*
+ * The {@link InjectorFactory} to be used can also be found using a {@link ServiceLoader} discovery
+ * mechanism as provided by {@link InjectorFactoryBuilder}.
+ *
* @author Erik Brakkee
*/
public class SimpleInjector {
- private InjectorCache cache = new InjectorCache(
- new CdiInjectorFactory());
+ private InjectorCache cache;
/**
* Constructs the injector.
- */
- public SimpleInjector() {
- cache = new InjectorCache(new CdiInjectorFactory());
- }
-
- /**
- * Construcst the injector.
* @param aFactory Factory to use.
*/
public SimpleInjector(InjectorFactory aFactory) {
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.wamblee.inject.Injector;
+import org.wamblee.inject.InjectorFactory;
+import org.wamblee.inject.SimpleInjector;
import org.wamblee.support.jndi.StubInitialContextFactory;
public class SimpleInjectorTest {
@Test
public void testGetSingleton() {
MyPojo pojo = new MyPojo();
- SimpleInjector injector = new SimpleInjector();
+ SimpleInjector injector = new SimpleInjector(new CdiInjectorFactory());
injector.inject(pojo);
MySingleton obj = pojo.getSingleton();
--- /dev/null
+<?xml version="1.0"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <parent>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-utils</artifactId>
+ <version>0.2.5-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-support-inject</artifactId>
+ <packaging>jar</packaging>
+ <name>/support/inject</name>
+ <url>http://wamblee.org</url>
+
+ <distributionManagement>
+ <site>
+ <id>support-inject</id>
+ <url>file:${distrib}/support/inject</url>
+ </site>
+ </distributionManagement>
+
+</project>
<url>http://wamblee.org</url>
<modules>
- <module>general</module>
+ <module>general</module>
+ <module>inject</module>
<module>cdi</module>
<module>spring</module>
</modules>