Spring AspectJ loadtimeweaving没有被调用

时间:2017-03-02 15:29:03

标签: java spring aspectj spring-aop

Spring AspectJ加载时编织配置正在构建和加载服务器而没有任何错误,但不会调用该方面。

以下是配置列表 1)JDK 8 2)服务器码头

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

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>

还尝试了aop.xml中的选项

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

方面

@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();
    }
}

注释

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

}

@Component
@Configurable
class TestClass implements ISomeInterface{

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

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

使用MAVEN_OPTS设置启动Jetty服务器

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

我的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>

未调用SampleAspect。我有几个使用@ExecuteByContext注释的方法(public,private和protected)。

2 个答案:

答案 0 :(得分:2)

可能您的目标类不是直接在包com.proj中,而是在子包中。包含子包的语法是..*,而不仅仅是.*,即在 aop.xml 中你应该有

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

等等。

答案 1 :(得分:0)

我终于通过以下更改解决了上述问题

包括-javaagent:/pathto/aspectjweaver-1.8.9.jar当我启动我的码头服务器时确保编织发生。

但我确实有几个方面有@Autowired bean,我需要评估Aspect中的某些条件。这些方面返回NullPointerException。原因是发生了弹簧组件扫描并且已经加载了类,并且加载时间编织没有机会编织,因为类已经加载了。

要在配置类中解决此问题,我添加了

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

添加此代码解决了NPE。

这篇文章非常有用 - 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/

并添加

-verbose -showWeaveInfo -debug

到aop.xml很有用

此外,我尝试转储编织的类以查看是否编织了一个类。更多细节 - https://eclipse.org/aspectj/doc/released/pdguide/printable.html#ltwdump