spring aop 注解 @Pointcut 同时切多个面
原创 bellys 发表于:2018-01-30 11:00:04
  阅读 :2242   收藏   编辑

因项目需求,做对系统做日志拦截,但因包路径不同,需要对2个不同包下的日志进行拦截

如:

com.xx.service
com.xx.mobile
com.xx.dao

而我只想拦截下面2个包和其子包的日志

com.xx.service
com.xx.mobile

经过网上查询和实践,总结如下;
在注解中通过“||”实现

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AOPTest {

    private static final Logger log = Logger.getLogger(LogAOP.class);  

    //定义一个切入点  
    @Pointcut(""
            + "execution( * com.xx.service..*.*(..)) || "
            + "execution( * com.xx.mobile..*.*(..))")  
    private void anyMethod(){}


    @Before("anyMethod()")  
    public void doBefore(JoinPoint joinPoint){  
         String method = joinPoint.getSignature().getDeclaringTypeName() + joinPoint.getSignature().getName();// 获得目标方法名
         log.info(method + "方法开始执行");  
    }  

    @After("anyMethod()")  
    public void doAfter(JoinPoint joinPoint){  
         String method = joinPoint.getSignature().getDeclaringTypeName() + joinPoint.getSignature().getName();// 获得目标方法名
         log.info(method + "方法执行结束");  
    }  



}