问题
Not sure what is going wrong, but AOP just doesn't seem to be working in my setup with spring boot (v1.1.6).
@Configuration
@ComponentScan
@EnableJpaRepositories
@EnableTransactionManagement
@EnableAutoConfiguration
@EnableCaching
@EnableLoadTimeWeaving
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
And in the aspect class
@Aspect
public class MyAspect {
@AfterReturning(pointcut = "execution(private * com.myapp.service.MyService.test(..)) && args(str1,str2)", argNames = "str1,str2")
public void advice(String str1, String str2) throws IOException {
System.out.println("Advising after returning");
}
}
In the service class that needs the advice
@Service
public class MyService {
public void test(String str1, String str2) throws IOException {
System.out.println("Test method in service");
//rest of the implementation
}
}
I also have a META-INF/aop.xml like so
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<!-- only weave classes in our application-specific packages -->
<include within="com.myapp.*"/>
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="com.myapp.aspect.MyAspect"/>
</aspects>
</aspectj>
When I run the application with -javaagent:path/to/spring-instrument-4.1.0.RELEASE.jar
I get this message on the console
2014-09-05 08:42:12.500 INFO 65053 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[AppClassLoader@58644d46] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified
2014-09-05 08:42:13.114 INFO 65053 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014-09-05 08:42:13.156 INFO 65053 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Registering beans for JMX exposure on startup
[AppClassLoader@58644d46] error can't determine implemented interfaces of missing type org.springframework.security.config.http.SessionCreationPolicy
when weaving type org.springframework.boot.actuate.autoconfigure.ManagementServerProperties$Security
when weaving classes
when weaving
[Xlint:cantFindType]
[AppClassLoader@58644d46] error can't determine implemented interfaces of missing type org.springframework.security.config.http.SessionCreationPolicy
when weaving type org.springframework.boot.actuate.autoconfigure.ManagementServerProperties$Security
when weaving classes
when weaving
[Xlint:cantFindType]
Nothing happens with the advice though. It won't fire.
Am I doing something wrong?
回答1:
I had the same problem. In my case I had to annotate my aspect class with the annotation @Component.
@Aspect
@Component
public class MyAspect {
...
}
The following link has a spring boot AOP example that helped me
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-aop
回答2:
Add below changes to your code and it should work. Do not need to create aop.xml. These changes will add the aspect to container and also weave it.
Read -- Enable Spring AOP or AspectJ
@Configuration
@ComponentScan
@EnableJpaRepositories
@EnableTransactionManagement
@EnableAutoConfiguration
@EnableCaching
@EnableAspectJAutoProxy -- Use this instead of @EnableLoadTimeWeaving
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Aspect
@Component/@Configurable -- Both of them work
public class MyAspect {
...
}
回答3:
In order to advise private methods you need to use a privileged aspect:
public privileged aspect MyAspect {
// ...
}
But the AspectJ documentation says:
Limitations: Privileged aspects are not supported by the annotation style.
So please use native syntax, not @AspectJ style. Before you do that, though, test if non-privileged, annotation-style aspects work as expected with public methods in order to exclude other reasons for your aspects being woven.
来源:https://stackoverflow.com/questions/25689814/spring-boot-aop-load-time-weaving