Spring autowiring using @Configurable Spring autowiring using @Configurable spring spring

Spring autowiring using @Configurable


You need to enable load-time weaving (or other kinds of weaving) in order to use @Configurable. Make sure you enabled it correctly, as described in 7.8.4 Load-time weaving with AspectJ in the Spring Framework.


I was having this problem with Tomcat 7 using LTW trying to autowire beans into my domain classes.

There was some updates to the doc for 3.2.x at http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/aop.html#aop-configurable-container that revealed that one can use @EnableSpringConfigured instead of the xml configuration .

So I have the following annotation on my Domain object:

@Configurable(preConstruction=true,dependencyCheck=true,autowire=Autowire.BY_TYPE)@EnableSpringConfigured

@EnableSpringConfigured is a substitue for

<context:spring-configured />

and don't forget to add this to your context xml file:

<context:load-time-weaver weaver-class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver" aspectj-weaving="on"/>

Of course I needed to setup Tomcat for load time weaving first.

Also, I ran into a bug in 3.2.0 (null pointer) so I needed to upgrade to Spring 3.2.1 (https://jira.springsource.org/browse/SPR-10108)

All is well now!


You should just look how Spring Roo does it since it does exactly what you want to do.

There are lots of things that can cause the NPE your having but most of the time it has to do with not compiling properly with AspectJ compiler and not having the Spring Aspects jar in your AspectJ lib path (this is different than your classpath).

First just try to get it to work with Maven and the AspectJ compiler plugin. Thats why I recommend Spring Roo as it will generate a POM file with the correct setup.

I have found @Configurable does not really work with LTW (despite one of the answers saying so). You need compile time weaving for @Configurable to work because the advice is happening at object construction time (constructor advice cannot be done with Springs LTW).