简体   繁体   English

Spring AspectJ loadtimeweaving未调用

[英]Spring AspectJ loadtimeweaving not invoked

The Spring AspectJ loadtime weaving configuration is building and loading server without any errors, but the aspect is not getting invoked. Spring AspectJ加载时织入配置正在构建和加载服务器,而没有任何错误,但是未调用该方面。

Here is the list of configuration 1) JDK 8 2) Server Jetty 以下是配置列表:1)JDK 8 2)服务器码头

@Configuration
@ComponentScan(basePackages = {..})
@EnableSpringConfigured
@EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.ENABLED)
@PropertySource(...)
@ImportResource(value = { "classpath:META-INF/aop.xml", ..})
class config {
...
}

aop.xml aop.xml

<aspectj>     
      <weaver options="-Xlint:ignore -Xset:weaveJavaPackages=true -Xset:weaveJavaxPackages=true">
        <include within="com.proj.*"/>
        <include within="java.*"/>
        <include within="javax.*"/>
        <include within="org.springframework.*"/>
        <include within="org.aspectj.*"/>
    </weaver>
    <aspects>
        <aspect name="com.proj.SampleAspect"/>
    </aspects>
</aspectj>

Have also tried with options in aop.xml 还尝试了aop.xml中的选项

options="-XnoInline -verbose -showWeaveInfo -debug -Xlint:ignore -Xset:weaveJavaPackages=true -Xset:weaveJavaxPackages=true"

Aspect 方面

@Component
@Aspect
public class SampleAspect {
    @Autowired
    private RequestContextFilter interceptRequestContext;

    @Around("@annotation(ExecuteByContext)")
    public Object interceptByContext(final ProceedingJoinPoint pjp) throws Throwable {
        if(SampleUtil.applyForRequest(interceptRequestContext.getRequestContext()) {
            LOGGER.info(String.format("Intercept context for method %s", pjp.getSignature().getName()));
            return null;
        }
        return pjp.proceed();
    }
}

Annotation 注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExecuteByContext {

}

@Component
@Configurable
class TestClass implements ISomeInterface{

  ...
  @ExecuteByContext
  public void method() {
    ..
  }

  @ExecuteByContext
  private void method1() {
    ..
  }
}

Jetty server is started with MAVEN_OPTS setting Jetty服务器以MAVEN_OPTS设置启动

-javaagent:/path_to/.m2/repository/org/springframework/spring-instrument/4.2.0.RELEASE/spring-instrument-4.2.0.RELEASE.jar -javaagent:/path_to/.m2/repository/org/springframework/spring-instrument/4.2.0.RELEASE/spring-instrument-4.2.0.RELEASE.jar

I have the following dependency in my maven 我的行家中有以下依赖项

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument</artifactId>
</dependency>

The SampleAspect is not getting invoked. SampleAspect没有被调用。 I have couple of methods (public, private and protected) annotated with @ExecuteByContext. 我有几种用@ExecuteByContext注释的方法(公共,私有和受保护)。

Probably your target classes are not directly in package com.proj but in a subpackage. 可能您的目标类不是直接在com.proj包中,而是在子包中。 The syntax for including subpackages is ..* , not simply .* , ie in your aop.xml you should have 包括子包的语法是..* ,而不是.* ,即在aop.xml中,您应该拥有

<include within="com.proj..*"/>

et cetera. 等等。

I finally solved the question above with the following changes 我终于通过以下更改解决了上述问题

including -javaagent:/pathto/aspectjweaver-1.8.9.jar when I start my jetty server ensured weaving happened. 当我启动码头服务器时,包括-javaagent:/pathto/aspectjweaver-1.8.9.jar可以确保编织成功。

But I did have few aspects that has @Autowired beans that I need to evaluating certain conditions in the Aspect. 但是我确实没有几个方面需要评估@Aspect中的某些条件的@Autowired bean。 These aspects where returning NullPointerException. 这些方面在哪里返回NullPointerException。 The reason was the spring component scan happened and the classes were already loaded and load time weaving did not have the opportunity to weave as the classes were already loaded. 原因是发生了spring组件扫描,并且类已经被加载,并且加载时间编织没有机会进行编织,因为类已经被加载。

To solve this in the config class I added 为了解决这个问题,我在config类中添加了

@Bean 
public SampleAspect sampleAspect() {
    SampleAspect aspect = Aspects.aspectOf(SampleAspect.class);
    return aspect;
}

adding this code solved the NPE. 添加此代码解决了NPE。

This article was very useful -- https://www.credera.com/blog/technology-insights/open-source-technology-insights/aspect-oriented-programming-in-spring-boot-part-3-setting-up-aspectj-load-time-weaving/ 这篇文章非常有用-https: //www.credera.com/blog/technology-insights/open-source-technology-insights/aspect-oriented-programming-in-spring-boot-part-3-setting-up -aspectj加载时间编织/

And adding 并添加

-verbose -showWeaveInfo -debug

to the aop.xml was helpful 对aop.xml很有帮助

Also I tried the dumping of the weaved classes to see if a class was weaved. 我也尝试转储编织的类,以查看是否编织了一个类。 More details here -- https://eclipse.org/aspectj/doc/released/pdguide/printable.html#ltwdump 此处有更多详细信息-https: //eclipse.org/aspectj/doc/released/pdguide/printable.html#ltwdump

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM