View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2013 Sebastian
3    * Thomschke.
4    *
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    *
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.integration.spring;
14  
15  import net.sf.oval.internal.Log;
16  
17  import org.springframework.beans.factory.annotation.Autowired;
18  import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
19  import org.springframework.stereotype.Component;
20  import org.springframework.util.Assert;
21  
22  /**
23   * Injects spring beans into unmanaged Java objects having {@link org.springframework.beans.factory.annotation.Autowired},
24   * {@link org.springframework.beans.factory.annotation.Value} and {@link javax.inject.Inject} annotations.
25   *
26   * <pre>
27   * &lt;bean class="net.sf.oval.integration.spring.SpringInjector" /&gt;
28   * </pre>
29   *
30   * or
31   *
32   * <pre>
33   * &lt;context:component-scan base-package="net.sf.oval.integration.spring" /&gt;
34   * </pre>
35   *
36   * @author Sebastian Thomschke
37   */
38  @Component
39  public class SpringInjector
40  {
41  	private static final Log LOG = Log.getLog(SpringInjector.class);
42  
43  	private static SpringInjector INSTANCE;
44  
45  	public static SpringInjector get()
46  	{
47  		Assert.notNull(INSTANCE, "No SpringInjector instance created yet. Add  <bean class=\"" + SpringInjector.class.getName()
48  				+ "\" /> to your spring configuration!");
49  
50  		return INSTANCE;
51  	}
52  
53  	@Autowired
54  	private AutowiredAnnotationBeanPostProcessor processor;
55  
56  	private SpringInjector()
57  	{
58  		LOG.info("Instantiated.");
59  
60  		INSTANCE = this;
61  	}
62  
63  	public void inject(final Object unmanagedBean)
64  	{
65  		processor.processInjection(unmanagedBean);
66  	}
67  }