Intercept private methods with Spring and AspectJ

本秂侑毒 提交于 2019-12-13 02:38:08

问题


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.


回答1:


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?



来源:https://stackoverflow.com/questions/23327336/intercept-private-methods-with-spring-and-aspectj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!