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注解的方法切面方法才会起作用

  • 相关阅读:
    Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function问题解决
    Fiddler是最强大最好用的Web调试工具之一--网站抓包分析
    django 运行不同的settings
    Ununtu 15.04 安装MySql(Django连接Mysql)
    Linux SSH登录服务器报ECDSA host key "ip地址" for has changed and you have requested strict checking.错误
    解决将Ubuntu下导出的requirements.txt到Centos服务器上面出现pkg-resource的版本为0.0.0
    Ubuntu安装Nginx和正确卸载Nginx Nginx相关
    jquery 情况form表单的所有内容
    python把中文文档变为拼音
    将多个文件夹内的txt合并
  • 原文地址:https://www.cnblogs.com/jiangwangxiang/p/11999846.html
Copyright © 2011-2022 走看看