我无法让 LTW 在带有嵌入式 Tomcat 的 Spring Boot 1.2.2 中工作。
我的应用程序是 WAR 文件,而不是 .JAR 文件。当我在 DEBUG 中运行时,即使点击应该与切入点匹配的调用,它也永远不会在我的方面停止,所以这就是我认为它不起作用的方式......
我的运行脚本这样做:
-javaagent:path/to/spring-instrument-xxx.jar -javaagent:path/to/aspectjweaver-1.2.8.jar
在 Spring Boot 中,我将此 AOP 配置作为 ApplicationInitializer 加载,因此它立即位于父 ApplicationContext 中,此后我的嵌入式 tomcat Web 应用程序上下文的所有其余部分都应该存在。
@EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.ENABLED)
@Configuration
public class AopConfig {
private Log log = LogFactory.getLog(AopConfig.class);
public AopConfig() {
log.info("Creating AopConfig");
}
@Bean
public LoadTimeWeaver loadTimeWeaver() {
log.info("Creating InstrumentationLoadTimeWeaver");
return new InstrumentationLoadTimeWeaver();
}
}
我的方面看起来像这样:
package my.aop.profiler.MethodTimerAspect;
@Aspect
public class MethodTimerAspect {
private static final String DELIMITER = "|";
private static final String PROFILER = "profiler";
private static final String DATE_FORMAT = "h:mm:ss";
private static final Log LOG = LogFactory.getLog(PROFILER);
public MethodTimerAspect() {}
@Pointcut("execution (* my.web.*Controller.*(..))")
protected void controllers() {}
@Pointcut("execution (* my.services..*Facade.*(..))")
protected void services() {}
@Pointcut("execution (* my.services..*Exchange.*(..))")
protected void data() {}
/**
* If profiling is enabled with trace, it will log the amount of time
* spent in the method
*
* @param joinPoint
* @return Object
* @throws Throwable
*/
@Around("controllers() || services() || data()")
public Object doProfiling(ProceedingJoinPoint joinPoint) throws Throwable {
// (...)
}
}
我的嵌入式 WAR 的 META-INF/aop.xml 是这样的:
<!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="cdot.*"/>
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="my.aop.profiler.MethodTimerAspect"/>
</aspects>
</aspectj>