Written from scratch Hystrix

1, springboot + custom annotation section for flexible configuration

Aop we can use to achieve business code and system-level services such as decoupled logging, transaction and security related business, our business code more clean and tidy.

First create a springboot project and prepare the controller

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 切面测试!"; } }

Section writing class, and configure the section

@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;
    }
}

So down we implemented a simple cut, to achieve their own data security certification in specific business logic section, where only a simple judgment, the test is as follows:

 

 

 

 Refine the aspect implement custom annotation

@Documented 
@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD)
public @interface MyAnnotation {

}
@Aspect 
@Component 
public  class TestAspect {
     / * * 
     methods * && added meet the specified rules and methods specified annotation section will be captured 
     * || method meet the specified rules or add a specified method will be annotated captured section 
     * / 
//     @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 comment section test! " ; 
    } 
}

 

 

 The actual development we can @Pointcut ( "execution (public * com.fanghao.web.SampleController.test * (..)) && @annotation (com.fanghao.aspect.MyAnnotation)") instead @Pointcut ( "execution (public * com.fanghao.web. *. * (..)) && @annotation (com.fanghao.aspect.MyAnnotation) ")", so that in com.fanghao.web package, we only add comment @MyAnnotation the method cut method will work

 

 

Guess you like

Origin www.cnblogs.com/jiangwangxiang/p/11999846.html