Spring Boot aspectJ LTW无法使用spring-instrument javaagent编写方法

时间:2018-03-24 20:01:52

标签: java spring spring-boot aop aspectj

我有使用此配置的spring boot应用程序:

@SpringBootApplication
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
@EnableLoadTimeWeaving
@EnableScheduling
@EnableConfigurationProperties({TelegramBotProperties.class, FreekassaProperties.class})
@EnableJpaRepositories(basePackages = {"com.birthright.telegrambot.repositories.postgres"}, enableDefaultTransactions = false)
public class Main {
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.setProtocol("org.apache.coyote.http11.Http11Nio2Protocol");
        return tomcat;
    }
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
    }
}

和aop.xml文件:

<?xml version="1.0"?>
<aspectj>
    <weaver options="-verbose -showWeaveInfo">
    </weaver>
</aspectj>

当我使用javaagent spring-instrument启动我的应用程序时,我在日志中看到了这个信息:

[AppClassLoader@18b4aac2] info using configuration /home/birthright/telegrambot/target/classes/META-INF/aop.xml
[AppClassLoader@18b4aac2] info using configuration file:/home/birthright/.m2/repository/org/springframework/spring-aspects/5.0.4.RELEASE/spring-aspects-5.0.4.RELEASE.jar!/META-INF/aop.xml
[AppClassLoader@18b4aac2] info register aspect org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect
[AppClassLoader@18b4aac2] info register aspect org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect
[AppClassLoader@18b4aac2] info register aspect org.springframework.transaction.aspectj.AnnotationTransactionAspect
[AppClassLoader@18b4aac2] info register aspect org.springframework.transaction.aspectj.JtaAnnotationTransactionAspect
[AppClassLoader@18b4aac2] info register aspect org.springframework.cache.aspectj.AnnotationCacheAspect
[AppClassLoader@18b4aac2] info register aspect org.springframework.cache.aspectj.JCacheCacheAspect

和weaveinfo:

[AppClassLoader@18b4aac2] weaveinfo Join point 'method-execution(void org.springframework.data.jpa.repository.support.SimpleJpaRepository.setRepositoryMethodMetadata(org.springframework.data.jpa.repository.support.CrudMethodMetadata))' in Type 'org.springframework.data.jpa.repository.support.SimpleJpaRepository' (SimpleJpaRepository.java:119) advised by around advice from 'org.springframework.transaction.aspectj.AnnotationTransactionAspect' (AbstractTransactionAspect.aj:66)
[AppClassLoader@18b4aac2] weaveinfo Join point 'method-execution(void org.springframework.data.jpa.repository.support.SimpleJpaRepository.deleteById(java.lang.Object))' in Type 'org.springframework.data.jpa.repository.support.SimpleJpaRepository' (SimpleJpaRepository.java:148) advised by around advice from 'org.springframework.transaction.aspectj.AnnotationTransactionAspect' (AbstractTransactionAspect.aj:66)
...

所以我看到只有 SimpleJpaRepository 被编织,但是com.birthright.telegrambot.services。*包中的 @Transactional 方法却没有。 我将aop.xml配置更改为:

 <weaver options="-verbose -showWeaveInfo">
        <include within="com.birthright.telegrambot.*"/>
    </weaver>

但现在甚至还没有使用SimpleJpaRepository。

在我尝试指定javaagent aspectjweaver-1.8.13并禁用spring注释 @EnableLoadTimeWeaving 之后(否则,应用程序无法启动)。 这次我的包中的所有方法都是编织的,但在应用程序的开头我看到了以下消息:

[AppClassLoader@18b4aac2] info AspectJ Weaver Version 1.8.13 built on Wednesday Nov 15, 2017 at 19:26:44 GMT
[AppClassLoader@18b4aac2] info register classloader sun.misc.Launcher$AppClassLoader@18b4aac2

然后app启动:

[MethodUtil@11d73f8] info AspectJ Weaver Version 1.8.13 built on Wednesday Nov 15, 2017 at 19:26:44 GMT
[MethodUtil@11d73f8] info register classloader sun.reflect.misc.MethodUtil@11d73f8 

这值得担心吗?为什么使用aspectjweaver代理工作正常,但使用spring-instrument和 @EnableLoadTimeWeaving 不是?

另一个令人尴尬的时刻。在类路径中,找到了spring-aspects jar的aop.xml,并注册了所有方面,例如 AnnotationTransactionAspect 。如果我将模式更改为AdviceMode.PROXY这是否意味着Spring将创建一个代理+ aspectJ以将其方面拧到Transactional方法?在这种情况下该怎么做,在自己的aop.xml中排除方面?

0 个答案:

没有答案