代码之家  ›  专栏  ›  技术社区  ›  LppEdd

AspectJ LTW(编织)不适用于弹簧靴

  •  0
  • LppEdd  · 技术社区  · 7 年前

    我在 Spring Boot 2.1.2.RELEASE - Java 11 - 脂肪罐

    根据文件,我有:

    将所需的依赖项添加到Gradle构建中

    implementation 'org.springframework.boot:spring-boot-starter-aop'
    implementation 'org.aspectj:aspectjrt:1.9.2'
    implementation 'org.aspectj:aspectjweaver:1.9.2'
    

    启用 LoadTimeWeaving

    @SpringBootApplication
    @EnableLoadTimeWeaving
    public class MyApplication { ... }
    

    提供了 aop.xml 在下面 META-INF

    <!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="my.base.package.*" />
        </weaver>
    
        <aspects>
            <!-- weave in just this aspect -->
            <aspect name="my.base.package.spring.aop.MyAspects" />
        </aspects>
    </aspectj>
    

    创建了新的 @Aspect

    @Aspect
    public class MyAspects {
        @Around("methodsToBeProfiled()")
        public Object profile(final ProceedingJoinPoint pjp) throws Throwable {
            final var sw = new StopWatch(getClass().getSimpleName());
            try {
                sw.start(pjp.getSignature().getName());
                return pjp.proceed();
            } finally {
                sw.stop();
                System.out.println(sw.prettyPrint());
            }
        }
    
        @Pointcut("execution(* my.base.package.other.MyClass.*(..))")
        public void methodsToBeProfiled() {}
    }
    

    添加了用于检测的jar

    -javaagent:/home/myuser/spring-instrument-5.1.5.RELEASE.jar
    

    日志,如你所见,显示 MyAspects 公认的

    [AppClassLoader@2c13da15] info AspectJ Weaver Version 1.9.2 built on Wednesday Oct 24, 2018 at 15:43:33 GMT
    [AppClassLoader@2c13da15] info register classloader jdk.internal.loader.ClassLoaders$AppClassLoader@2c13da15
    [AppClassLoader@2c13da15] info using configuration /home/edoardo/IdeaProjects/scheduler/scheduler-engine/out/production/resources/META-INF/aop.xml
    [AppClassLoader@2c13da15] info using configuration file:/home/edoardo/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aspects/5.1.5.RELEASE/3bb95e05b646ef93e2a4cf0b600924c2979fc723/spring-aspects-5.1.5.RELEASE.jar!/META-INF/aop.xml
    [AppClassLoader@2c13da15] info register aspect my.base.package.spring.aop.MyAspects
    [AppClassLoader@2c13da15] info register aspect org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect
    [AppClassLoader@2c13da15] info register aspect org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect
    [AppClassLoader@2c13da15] info register aspect org.springframework.transaction.aspectj.AnnotationTransactionAspect
    [AppClassLoader@2c13da15] info register aspect org.springframework.transaction.aspectj.JtaAnnotationTransactionAspect
    [AppClassLoader@2c13da15] info deactivating aspect 'org.springframework.transaction.aspectj.JtaAnnotationTransactionAspect' as it requires type 'javax.transaction.Transactional' which cannot be found on the classpath
    [AppClassLoader@2c13da15] info register aspect org.springframework.cache.aspectj.AnnotationCacheAspect
    [AppClassLoader@2c13da15] info register aspect org.springframework.cache.aspectj.JCacheCacheAspect
    [AppClassLoader@2c13da15] info deactivating aspect 'org.springframework.cache.aspectj.JCacheCacheAspect' as it requires type 'org.springframework.cache.jcache.interceptor.JCacheAspectSupport' which cannot be found on the classpath
    [AppClassLoader@2c13da15] info deactivating aspect 'org.springframework.cache.aspectj.JCacheCacheAspect' as it requires type 'javax.cache.annotation.CacheResult' which cannot be found on the classpath
    [AppClassLoader@2c13da15] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified
    

    然而 我的方面 从来没有被实例化(所以没有调试),并且方法也没有被我的方面代码编织。

    我错过什么了吗?


    编辑:好像两个罐子, aspectjweaver spring-instrument 必须是代理。这正常吗?

    0 回复  |  直到 7 年前
        1
  •  1
  •   Dave Syer    7 年前

    你确实需要 aspectjweaver 代理jar。你不需要 spring-instrument (对于胖jar-afaik来说,它曾在应用服务器中用于解决类加载程序的一些历史问题)。你也不需要 @EnableLoadTimeWeaving (也是一个应用服务器功能,如果您控制代理则是多余的)。还有(小麻烦) aspectjrt 是的传递依赖项 AspectJweaver公司 ,所以你不需要两者都有。所以看起来你已经开始工作了,尽管你已经做了比你需要的更多的工作。具有各种编织选项的示例Spring Boot应用程序: here .