Dynamic dependency injection with Spring

Out of the box, Spring provides various techniques to define your beans and their dependencies - XML files, Java annotations and since version 4.0, even using groovy scripts. Furthermore, you can control the creation of the beans using FactoryBean and bean post-processors. Still, there are times when it would be very convenient to create an instance of some random class and have its dependencies satisfied at runtime without Spring knowing anything about that class a priori.

If you are willing to accept a few limitations - only bean dependencies, not simple scalar properties, and only those annotated with @javax.inject.Inject, then the following code creates a dependency injector that you can use to dynamically “bind” random objects.

The dependency injector itself should be a spring bean and available to your code that wants to inject something else. In addition, you need to have an instance of AutowiredAnnotationBeanPostProcessor. This bean is automatically included if you have the annotation-config directive in your Spring context file:

	<context:annotation-config />

You can now use the dependency injector to bind dependencies at runtime as follows:

	public class MyObject {
		@Inject SomeOtherObject other; // SomeOtherObject is spring managed

        ...
    }


	...
    MyObject withDependencies = dependencyInjector.bind(new MyObject());