问题
The Spring AspectJ loadtime weaving configuration is building and loading server without any errors, but the aspect is not getting invoked.
Here is the list of configuration 1) JDK 8 2) Server Jetty
@Configuration
@ComponentScan(basePackages = {..})
@EnableSpringConfigured
@EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.ENABLED)
@PropertySource(...)
@ImportResource(value = { "classpath:META-INF/aop.xml", ..})
class config {
...
}
aop.xml
<aspectj>
<weaver options="-Xlint:ignore -Xset:weaveJavaPackages=true -Xset:weaveJavaxPackages=true">
<include within="com.proj.*"/>
<include within="java.*"/>
<include within="javax.*"/>
<include within="org.springframework.*"/>
<include within="org.aspectj.*"/>
</weaver>
<aspects>
<aspect name="com.proj.SampleAspect"/>
</aspects>
</aspectj>
Have also tried with options in aop.xml
options="-XnoInline -verbose -showWeaveInfo -debug -Xlint:ignore -Xset:weaveJavaPackages=true -Xset:weaveJavaxPackages=true"
Aspect
@Component
@Aspect
public class SampleAspect {
@Autowired
private RequestContextFilter interceptRequestContext;
@Around("@annotation(ExecuteByContext)")
public Object interceptByContext(final ProceedingJoinPoint pjp) throws Throwable {
if(SampleUtil.applyForRequest(interceptRequestContext.getRequestContext()) {
LOGGER.info(String.format("Intercept context for method %s", pjp.getSignature().getName()));
return null;
}
return pjp.proceed();
}
}
Annotation
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExecuteByContext {
}
@Component
@Configurable
class TestClass implements ISomeInterface{
...
@ExecuteByContext
public void method() {
..
}
@ExecuteByContext
private void method1() {
..
}
}
Jetty server is started with MAVEN_OPTS setting
-javaagent:/path_to/.m2/repository/org/springframework/spring-instrument/4.2.0.RELEASE/spring-instrument-4.2.0.RELEASE.jar
I have the following dependency in my maven
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
</dependency>
The SampleAspect is not getting invoked. I have couple of methods (public, private and protected) annotated with @ExecuteByContext.
回答1:
Probably your target classes are not directly in package com.proj
but in a subpackage. The syntax for including subpackages is ..*
, not simply .*
, i.e. in your aop.xml you should have
<include within="com.proj..*"/>
et cetera.
回答2:
I finally solved the question above with the following changes
including -javaagent:/pathto/aspectjweaver-1.8.9.jar when I start my jetty server ensured weaving happened.
But I did have few aspects that has @Autowired beans that I need to evaluating certain conditions in the Aspect. These aspects where returning NullPointerException. The reason was the spring component scan happened and the classes were already loaded and load time weaving did not have the opportunity to weave as the classes were already loaded.
To solve this in the config class I added
@Bean
public SampleAspect sampleAspect() {
SampleAspect aspect = Aspects.aspectOf(SampleAspect.class);
return aspect;
}
adding this code solved the NPE.
This article was very useful -- https://www.credera.com/blog/technology-insights/open-source-technology-insights/aspect-oriented-programming-in-spring-boot-part-3-setting-up-aspectj-load-time-weaving/
And adding
-verbose -showWeaveInfo -debug
to the aop.xml was helpful
Also I tried the dumping of the weaved classes to see if a class was weaved. More details here -- https://eclipse.org/aspectj/doc/released/pdguide/printable.html#ltwdump
来源:https://stackoverflow.com/questions/42559468/spring-aspectj-loadtimeweaving-not-invoked