为什么切入点只在@Controller bean中触发但在其他没有@Controller注释的bean中不起作用?

时间:2017-06-23 10:08:14

标签: java spring spring-mvc aop

我的配置与此问题相同:why did the adivce not work in my project based on Spring-framework?

我发现切入点不匹配的原因是带注释的bean @Service具有@Transactional注释。

我尝试在@Pointcut bean中添加一个匹配方法的@Controller,并使用此切入点正常调用注释@Before的相应方法。但是当我尝试添加@Pointcut匹配bean内部没有@Controller注释的方法时,没有调用带注释@Before的相应方法。

代码和其他情况如下:

方面

@Component
@Aspect
public class RecordActivity{
    @Pointcut("execution(void test.article.handler.PublicHandler.addArticle(..))")
    public void addControllArticle(){};

    @Pointcut("execution(void   test.article.serviceAspectWrap.ArticleServiceWrap.*(..))")
    public void addWrapArticle(){};

    @Pointcut("execution(public void test.article.service.ArticleService.*(..))")
    public void addServiceArticle(){};

    //this method was invoked normally
    @Before("addControllArticle()")
    public void addarticleActivity(){
        System.out.println("------add an Controllarticle-------");
    }

    //this method did not be invoked
    @Before("addWrapArticle()")
    public void addarticleActivity(){
        System.out.println("------add an Wraparticle-------");
    }

    //this method did not be invoked
    @Before("addSerivceArticle()")
    public void addarticleActivity(){
        System.out.println("------add an Servicearticle-------");
    }
}

控制器

@Controller    
public class PublicHandler {

//this method was visited by webbrowser and could feed back a normal result.

@RequestMapping("/addarticle")
public void addArtcile( HttpServletRequest request,HttpServletResponse response,Article article){  


articleService.addArticle(article);
...
}

服务

@Service
public class ArticleService {

    //this method was visited by webbrowser and could feed back a normal result.
    @Transactional(rollbackFor=Exception.class)
    public void addArticle(HttpServletRequest request,HttpServletResponse response,Article article){
                .....
    }
}

ServiceWrap

@Component
public class ArticleServiceWrap{
 @Autowired
     ArticleService articleService;`

     //this method was visited by webbrowser and could feed back a normal result.
     public void addArticle( HttpServletRequest request,HttpServletResponse response,Article article){
        articleService.addArticle(request, response, article);
    }

没有错过要扫描的包裹。 这让我非常困惑。请告诉我原因。谢谢提前!

1 个答案:

答案 0 :(得分:0)

我找到了原因。在test.article.serviceAspectWrap.ArticleServiceWrap类被scaned后,test.aop包被扫描。我将它们反转并调用了注释@Before("addWrapArticle()")的方法。