Spring Boot加载时间编织在嵌入式tomcat中不起作用

时间:2015-03-16 22:22:12

标签: spring-boot aspectj load-time-weaving embedded-tomcat-8

我无法让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 Config作为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 {
        // (...)
    }
}

My Embedded 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>

1 个答案:

答案 0 :(得分:0)

两个想法:

  • 您可能希望更改其中一个切入点以查找子包(使用..语法而不是.):

    @Pointcut("execution (* my.web..*Controller.*(..))")
    
  • 这同样适用于您的 aop.xml

    <include within="cdot..*"/>
    

我认为my.web被您故意更改并实际读取cdot.something,否则切入点将无法匹配。