【问题标题】:Advice not getting called in Spring-AOP在 Spring-AOP 中没有被调用的建议
【发布时间】:2012-08-20 20:33:31
【问题描述】:

我已经声明了以下建议 dao 调用的 Aspect,我正在尝试运行 @Before 建议但它不起作用。

这里是方面。

package com.hedgebenefits.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AccessControlAspect {
    @Before("within(com.hedgebenefits.daos..*) && execution(public * *(..))")
    public void daoCall() {
        System.out.println("Before advice invoked for DAO method called ");
    }
}

我的 application-context.xml 有以下标签

<aop:aspectj-autoproxy/>

我的Dao类如下:

package com.hedgebenefits.daos.impl;

import com.hedgebenefits.daos.AdminDao;
import com.hedgebenefits.domain.Admin;
import org.springframework.stereotype.Repository;

@Repository
public class AdminDaoImpl implements AdminDao{
    @Override
    public void save(Admin admin) {
    }
}

我放了一个断点,但我可以看到它没有激活,我肯定在这里犯了一些愚蠢的错误,但无法弄清楚。 PL。建议。

【问题讨论】:

    标签: spring spring-aop


    【解决方案1】:

    您的方面需要成为应用程序上下文的一部分。

    1. 如果您使用component-scan,请将@Component 添加到您的AccessControllerAspect,或设置component-scan 过滤器以包含@Aspect 注释。要设置过滤器,请查看section 3.10.3 of the Spring documentation (Using filters to customize scanning)
    2. 如果您使用的是 xml 配置,请为 AccessControllerAspect 添加一个 bean。

    添加aop:aspectj-autoproxy 的行为是不够的。这会告诉 已经成为应用程序上下文的一部分的 bean 如何进行切面,它不会自动包含它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-15
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多