使用Spring和AspectJ拦截私有方法 [英] Intercept private methods with Spring and AspectJ

查看:656
本文介绍了使用Spring和AspectJ拦截私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在使用私有方法使用AspectJ的加载时编织和基于Spring-Boot以及基于注释的配置之前执行代码,并且试图找出为什么未调用我的方面的原因./p>

我的简单方面如下:

@Aspect
public class LoggingAspect {

  private static Logger log = LoggerFactory.getLogger(LoggingAspect.class);

  @Before("execution(private * com.mycompany.MyServiceWithPrivateMethods.*(..))")
  public void privateAspect() {
    log.warn("#### Private method aspect invoked!");
  }

}

我的Spring配置类上还有@EnableLoadTimeWeaving批注,在META-INF/aop.xml中也有以下批注:

<aspectj>    
    <weaver options="-verbose">
        <!-- only weave classes in our application-specific packages -->
        <include within="com.mycompany.*"/>
    </weaver>

    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="com.mycompany.LoggingAspect"/>
    </aspects>    
</aspectj>

最后,根据文档,我还使用-javaagent:/path/to/spring-instrument.jar选项启动了我的应用程序.我在类路径(spring-aopaspectjweaver)上也有必需的jar,如Spring文档中所述.

这甚至有可能吗?在AspectJ文档中列出了一个限制",指出位于Privileged aspects are not supported by the annotation style. ,但是较早的帖子似乎表明确实有可能.

解决方案

我本以为可以,但是我也以为您必须将Aspectjweaver.jar添加为javaagent.这些假设之一是错误的?

I am trying to execute code before a private method using AspectJ's load-time-weaving with Spring-Boot and annotation-based configuration and I'm pulling my hair out trying to figure out why my aspect is not being invoked.

My simple aspect is as follows:

@Aspect
public class LoggingAspect {

  private static Logger log = LoggerFactory.getLogger(LoggingAspect.class);

  @Before("execution(private * com.mycompany.MyServiceWithPrivateMethods.*(..))")
  public void privateAspect() {
    log.warn("#### Private method aspect invoked!");
  }

}

I also have the @EnableLoadTimeWeaving annotation on my Spring configuration class and the following in my META-INF/aop.xml:

<aspectj>    
    <weaver options="-verbose">
        <!-- only weave classes in our application-specific packages -->
        <include within="com.mycompany.*"/>
    </weaver>

    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="com.mycompany.LoggingAspect"/>
    </aspects>    
</aspectj>

Lastly, I am also starting my application with the -javaagent:/path/to/spring-instrument.jar option, per the docs. I also have the required jars on my classpath (spring-aop and aspectjweaver) as also mentioned in the Spring docs.

Is this even possible? There is a 'limitation' listed in the AspectJ docs that states Privileged aspects are not supported by the annotation style. located here, however this older post in the Spring Forum seems to indicate that it is indeed possible.

解决方案

I would have thought it would work, but I would have also thought you would have to add aspectjweaver.jar as a javaagent. One of those assumptions is wrong?

这篇关于使用Spring和AspectJ拦截私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆