注解方式实现AOP

1、 添加依赖

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2、定义注解

@Documented
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Mapping
public @interface AclAnnotation {
	public String value() default "";
}

3、配置AOP切面

@Aspect
@Configuration
public class AopConfig {

    @Autowired
    private AccessControlService accessControlService;

    //切入点1(根据注解定位)
    @Pointcut("@annotation(com.traffic.annotation.AclAnnotation)")
    public void pointCut(){};
    
    //切入点2(根据类定位)
	@Pointcut("execution(* springMVCmybatis.com.my.aop.UserServiceImp.*(..))") 
    private void testAOP() {}
    
    /*
     *  声明前置通知 ,JoinPont是srpring提供的静态变量,
     *  通过joinPoint参数可以获得目标方法的类名,方法参数,方法名等信息,这个参数可有可无。
     *  注解中的参数也可不使用切点,直接 @Before(value = "execution(* springMVCmybatis.com.my.aop.UserServiceImp.*(..))")
     */
    @Before("pointCut() || testAOP()") 
    public void doBefore(JoinPoint joinPoint) { 
        System.out.println("@Before:开始添加--order=3"); 
    } 
       
    /*
     *声明后置通知 ,如果result的类型与proceed执行的方法返回的参数类型不匹配那么就不会执行这个方法 
     *注解中的参数也可不使用切点
     */
    @AfterReturning(pointcut = "pointCut()  || testAOP()", returning = "result") 
    public void doAfterReturning(String result) { 
        System.out.println("@AfterReturning:后置通知--order=3"); 
        System.out.println("---" + result + "---"); 
    } 
       
    /*
     *声明例外通知 
     *注解中的参数也可不使用切点
     */
    @AfterThrowing(pointcut = "pointCut() || testAOP()", throwing = "e") 
    public void doAfterThrowing(Exception e) { 
        System.out.println("@AfterThrowing:例外通知--order=3"); 
        System.out.println(e.getMessage()); 
    } 
       
    //声明最终通知 
    @After("pointCut() || testAOP()") 
    public void doAfter() { 
        System.out.println("@After:最终通知--order=3"); 
    } 
        
    /*
     * 声明环绕通知
     * 参数必须是ProceedingJoinPoint,通过该对象的proceed()方法来执行目标函数,
     * proceed()的返回值就是环绕通知的返回值,proceedingJoinPoint是个接口,
     * implement JoinPoint,所以也可以获得目标函数的类名,方法名等参数。
     */
    @Around("pointCut() || testAOP()")
    public PageQueryResponse around(ProceedingJoinPoint pjp){
    	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        
        PageQueryResponse response = new PageQueryResponse();
        Signature signature = pjp.getSignature();

        MethodSignature methodSignature = (MethodSignature)signature;
        Method targetMethod = methodSignature.getMethod();
        if(targetMethod.isAnnotationPresent(AclAnnotation.class)){

            AclAnnotation aclAnnotation = (AclAnnotation)targetMethod.getAnnotation(AclAnnotation.class);
            //权限名称
            String aclName = aclAnnotation.aclName();
			response = (PageQueryResponse)pjp.proceed();
        }
        return response;
    }



}

4、使用切面

在类或方法上添加@AclAnnotation("ae_traffic_bops_f_edit_data")即可
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值