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

标签 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 配置作为 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>

最佳答案

两个想法:

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

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

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

我假设 my.web 是您故意更改的,实际上读取的是 cdot.something,否则切入点将不匹配。

关于spring-boot - Spring Boot 加载时间编织在嵌入式 tomcat 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29088032/

相关文章:

java - 签名 jar 的 AspectJ 加载时编织

java - Spring Boot @Scheduled 不适用于数据库事务

java - Spring 的 AspectJ NoSuchMethodError

kotlin - @Around 方面和 Kotlin 挂起函数

java - AspectJ AOP LTW 不适用于 javaagent 的动态加载

java - 使用 AspectJ LTW 时的 Spring 缓存问题

java - Spring 启动+ webflux : context lost when running some steps in parallel

java - Hibernate 中的 Longvarchar

spring - @JsonManagedReference 和 @JsonBackReference 无法从 jsonbackreference 模型中获取关系数据

java - 检查建议是否得到应用