2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.inject;
20 * The main entry point for programmatic dependency injection. A different
21 * {@link InjectorFactory} can be plugged in for testing.
24 * Given the following class:
29 * private Service service;
35 * injecting the EJB into a POJO is accomplished as follows:
38 * Pojo pojo = new Pojo();
39 * SimpleInjector injector = new SimpleInjector(InjectorFactoryBuilder
40 * .getInjectorFactory());
41 * injector.inject(pojo);
44 * Of course, the above example assumes the the injector understands the
45 * @EJB annotation (which of course CDI does).
47 * The <code>SimpleInjector</code> should be cached. This is because the
48 * <code>SimpleInjector</code> caches the {@link Injector} objects that it uses
49 * internally for performance. This is done because creation of these internal
50 * <code>Injector</code> objects may be costly. Caching the simple injector makes sure
51 * that a class is not analysed again for annotations every time injection is
54 * For more advanced cases, the injector factory can also be directly
55 * constructed instead of being obtained through the
56 * {@link InjectorBuilder}.
58 * @author Erik Brakkee
60 public class SimpleInjector implements Injector {
62 private InjectorCache cache;
65 * Constructs the injector.
70 public SimpleInjector(InjectorFactory aFactory) {
71 cache = new InjectorCache(aFactory);
75 * Injects into a given object.
78 * Object to inject into.
80 public void inject(Object aObject) {
81 cache.getInjector(aObject.getClass()).inject(aObject);