Monday, May 9, 2011

Autowiring objects not instantiated by the Spring Framework

In order to enabled autowiring capabilities, add the following properties to the beans element.
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context"


And make sure this line is included within the beans element
<context:annotation-config />

Next, in your code, get an instance of AutowireCapableBeanFactory.

Hint: Subclasses of AbstractApplicationContext have a getAutowireCapableBeanFactory, just one way to get such an instance.

Finally call the autowireBean(MyObject) method and like magic, so long as the annotations are correct, Spring will inject the correct instances.

Thursday, May 5, 2011