zoukankan      html  css  js  c++  java
  • 从零开始写Hystrix

    1、springboot+自定义注解实现灵活的切面配置

    利用aop我们可以实现业务代码与系统级服务例如日志记录、事务及安全相关业务的解耦,使我们的业务代码更加干净整洁。

    首先创建一个springboot项目,并编写控制器

    package com.fanghao.web;

    @RestController
    public class SampleController { @RequestMapping("/test11") public int test11(String deviceId) { return 11; } @RequestMapping("/testGG") public String testGG(String deviceId) { return "testGG 切面测试!"; } }

    编写切面类,并配置切面

    @Aspect
    @Component
    public class TestAspect {
        @Pointcut("execution(public * com.fanghao.web.SampleController.test*(..))")
        public void addAdvice(){}  
        
        @Around("addAdvice()")
        public Object Interceptor(ProceedingJoinPoint pjp){
            Object result = null; 
            Object[] args = pjp.getArgs();
            if(args != null && args.length >0) {
                String deviceId = (String) args[0];
                 if(!"03".equals(deviceId)) {
                     return "no anthorization";
                 }
            }     
            try {
                result =pjp.proceed();
            } catch (Throwable e) {
                e.printStackTrace();
            }  
            return result;
        }
    }

    这样下来我们就实现了一个简单切面,在切面中实现自己的数据安全认证等具体业务逻辑,此处只做一个简单判断,测试如下:

     

     改进切面,实现自定义注解

    @Documented 
    @Retention(RetentionPolicy.RUNTIME) 
    @Target(ElementType.METHOD)
    public @interface MyAnnotation {
    
    }
    @Aspect
    @Component
    public class TestAspect {
        /**
         * && 满足指定规则的方法并且加了指定注解的方法才会被切面捕捉
         * || 满足指定规则的方法或者加了指定注解的方法才会被切面捕捉
         */
    //    @Pointcut("execution(public * com.fanghao.web.SampleController.test*(..))")
    //    @Pointcut("execution(public * com.fanghao.web.SampleController.test*(..)) && @annotation(com.fanghao.aspect.MyAnnotation)")
        @Pointcut("execution(public * com.fanghao.web.SampleController.test*(..)) || @annotation(com.fanghao.aspect.MyAnnotation)")
        public void addAdvice(){}  
        
        @Around("addAdvice()")
        public Object Interceptor(ProceedingJoinPoint pjp){
            Object result = null; 
            Object[] args = pjp.getArgs();
            if(args != null && args.length >0) {
                String deviceId = (String) args[0];
                 if(!"03".equals(deviceId)) {
                     return "no anthorization";
                 }
            }     
            try {
                result =pjp.proceed();
            } catch (Throwable e) {
                e.printStackTrace();
            }  
            return result;
        }
    }
    @RestController
    public class SampleController {
        
        @RequestMapping("/test11")
        public int test11(String deviceId) {
            return 11;
        }
        @RequestMapping("/testGG")
        public String testGG(String deviceId) {
            return "testGG 切面测试!";
        }
        @MyAnnotation
        @RequestMapping("/showDD")
        public String showDD(String deviceId) {
            return "showDD 注解切面测试!";
        }
    }

     

     实际开发中我们可以将@Pointcut("execution(public * com.fanghao.web.SampleController.test*(..)) && @annotation(com.fanghao.aspect.MyAnnotation)")改为@Pointcut("execution(public * com.fanghao.web.*.*(..)) && @annotation(com.fanghao.aspect.MyAnnotation)")",这样在com.fanghao.web包下,只有我们加上@MyAnnotation注解的方法切面方法才会起作用

  • 相关阅读:
    GDI+重绘笔记
    VC++动态链接库(DLL)编程深入浅出(四)
    VC++动态链接库(DLL)编程深入浅出(三)
    VC++动态链接库(DLL)编程深入浅出(二)
    VC++动态链接库(DLL)编程深入浅出(一)
    AOP在 .NET中的七种实现方法
    [c#菜鸟]lambda表达式
    C# IP地址与整数之间的转换
    log4net在xp下不能写日志到mdb,报错Could not load type 'System.Security.Claims.ClaimsIdentity'
    Active Directory 域服务之AD DS 逻辑组件概述
  • 原文地址:https://www.cnblogs.com/jiangwangxiang/p/11999846.html
Copyright © 2011-2022 走看看