问题
I'm having trouble getting AspectJ to perform load time weaving on a class annotated with @configurable in my main project. No fields get set and none of the setters are touched.
I don't think there's trouble with the configuration itself, because I've extracted the configuration and tested it on a smaller sandbox project. Just for the sake of it, I'll include it in this question though.
So, I'm wondering:
- Is there anything in the larger project that might be hindering Spring/AspectJ from detecting the this particular class?
- Is there any way of checking if spring is even aware of the class in questions?
And lastly, whatever code I can extract (please excuse the obfuscation):
From configuration XML:
<context:annotation-config />
<context:spring-configured />
<context:component-scan base-package="se.isydev" />
<context:component-scan base-package="se.istools" />
<aop:aspectj-autoproxy />
<context:load-time-weaver aspectj-weaving="on" />
<context:property-placeholder location="classpath:settings.properties" />
(...)
<bean class="com.company.ClassToBeWeaved"
scope="prototype">
<property name="injectedBean" ref="injectedBean" />
</bean>
And the class itself:
@Configurable
public class ClassToBeWeaved {
private InjectedBean injectedBean;
@Required
public void setInjectedBean() { ... }
}
Edit:
Well, turns out that it wasn't working due to a circular dependency. Oh deary me, I love working on legacy code. Still, my original questions remain.
回答1:
You possibly have forgotten to "weave". Add -javaagent:path/to/aspectjweaver.jar
or -javaagent:path/to/spring-agent.jar
to your comman line.
I also suggest that you @Autowire
your dependency rather than explicitly inject it.
回答2:
I believe the LTW requires a META-INF/aop.xml on your classpath. It should look like:
<aspectj>
<!--
Uncomment this is you need AOP logging <weaver options="-verbose
-showWeaveInfo
-XmessageHandlerClass:org.springframework.aop.aspectj.AspectJWeaverMessageHandler">
-->
<weaver>
<include within="com.xxx.MyClass" />
</weaver>
<aspects>
<aspect name="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"/>
<include within="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"/>
</aspects>
</aspectj>
回答3:
Do you have more then 1 spring XML file? I believe I had a problem <aop:aspectj-autoproxy /> was not in the 'most parent' of my XML files.
回答4:
Some hints for your question.
To make load time weaving work with Spring, not only you need to configure properly the aop.xml but you need ALSO to have the spring-instrument.jar & spring-aspects.jar.
These jar files contain their own aop.xml that declare Spring aspects to handle:
- @Transactional support
- @Configurable support
- JPA Exception translation support
- @Async annotation for scheduling support
What happen in the background ?
When using AspectJ load-time weaving, the @Transactional and @Configurable implementations are no longer based on JDK proxies or CGLIB proxies but real AspectJ aspects.
To enable these real aspects, you need the additional jar files. The jar also contain declaration of these aspects in their own aop.xml
More details on how to integrate Spring with AspectJ here
来源:https://stackoverflow.com/questions/1389760/spring-load-time-weaving-not-detecting-class-annotated-with-configurable