【问题标题】:AspectJ AOP not working as expectedAspectJ AOP 没有按预期工作
【发布时间】:2015-07-06 13:13:52
【问题描述】:

我正在 Spring MVC 应用程序上实现 AspectJ AOP。我已经编写了 Aspect java 类,我试图截取其中一个包的所有方法的连接点,比如 com.xyz.services。但是AOP总是无法拦截那个包的方法。方面定义如下-

@Pointcut("execution(* com.xyz.services..*.*(..))")
public void logBefore() {

}
@Before("logBefore()")
public void logHere(JoinPoint joinPoint) {
    System.out.println("In logHere ....");
    logger.info("logBefore is running ....");
    logger.info("hijacked ::::" + joinPoint.getSignature().getName());
    logger.info("joinPoint.getSignature().getDeclaringTypeName() ::::"
                    + joinPoint.getSignature().getDeclaringTypeName());
    logger.info("joinPoint.getSignature().getModifiers() ::::"
            + joinPoint.getSignature().getModifiers());
    logger.info("******************************************************");
}

我在 application-context.xml 中启用了 AOP 如下 -

<aop:aspectj-autoproxy proxy-target-class="true">
    <aop:include name='loggingAspect' />
</aop:aspectj-autoproxy>

每当我调用 webservice 时,com.xyz.services 中的方法都会被调用,但 Aspect 方法永远不会被调用。

我尝试了不同的切入点,但方面代码从未执行过。

@Pointcut("execution(public * com.xyz.services.ManagerServiceImpl.*(..))")
public void logBefore() {
System.out.println("In Logbefore");}

@Pointcut("execution(public * com.xyz.services.*.*(..))")
public void logBefore() {
System.out.println("In Logbefore");
}    

我添加了对 pom.xml 的 cglib 依赖项以启用基于 cglib 的代理。

<dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>

谁能帮我弄清楚为什么这些方面没有按预期工作?

【问题讨论】:

  • 各位有什么意见吗?
  • 可能因为信息不完整,没有答案。你能准备一点SSCCE,也许在 GitHub 上,包括一个 Maven POM,以便让你的问题可以重现?切入点看起来不错,因此要么是配置问题,要么是您的建议所针对的类不是 Spring 组件,或者是您的类中的一个微妙的包名称问题。如果没有更多信息,我只能假设。

标签: spring-mvc aspectj


【解决方案1】:

您是否正确配置了带有注释的类?

@EnableAspectJAutoProxy(proxyTargetClass = true)

【讨论】:

    猜你喜欢
    • 2021-11-01
    • 1970-01-01
    • 2015-05-19
    • 2021-10-19
    • 2020-03-18
    • 2012-06-14
    • 2014-11-15
    • 1970-01-01
    • 2012-07-02
    相关资源
    最近更新 更多