zoukankan      html  css  js  c++  java
  • 【AOP】基于@Aspect的AOP配置

    基于spring cloud的aop配置

    1,启动类MemberAppliaction增加注解

    @Import({SwaggerConfiguraion.class, WebMvcAutoConfiguration.class})
    @SpringBootApplication
    @FFanApplication
    @EnableFFanApiDoc
    @EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy=true)  //启动aspect的aop注解
    @EnableAutoConfiguration
    @EnableTransactionManagement
    @EnableDiscoveryClient
    @ComponentScan(basePackages = {"cn.wanda.sail.member"})
    @MapperScan(basePackages = {"cn.wanda.sail.member.mapper"})
    @EnableFeignClients(basePackages = {"cn.wanda.sail.member.client"})
    public class MemberApplication {

    2,定义切面类

    @Aspect
    @Component
    public class ProcessorAspect {
    
        private static final Logger log = LoggerFactory.getLogger(ProcessorAspect.class);

    3,定义切入方法

        @Around("execution(public * cn.wanda.sail.member.task.support.MemberTask.process(..))")  //环绕增强
        public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
            if (joinPoint.getArgs() == null || joinPoint.getArgs().length == 0) {
                return joinPoint.proceed();
            }
            TaskContext task = (TaskContext) joinPoint.getArgs()[0];
            log.info("LOG00020: {}  start.....", task.getTaskId());
            long start = System.currentTimeMillis();
            Object result = null;
            try {
                result = joinPoint.proceed();   //执行目标方法
            } catch (Exception exception) {
                handleException(task.getTaskId(), exception);
            }
            log.info("LOG00040: {} end cost : {} ms", task.getTaskId(), (System.currentTimeMillis() - start));
    
            return result;
        }

    4,常用的切入方法

       (1),后置返回通知

                @AfterReturning("execution(* com.sxit..*.*(..))")  

       (2),后置异常通知

                @AfterThrowing("execution(* com.sxit..*.*(..))")  

       (3),后置最终通知

                @After("execution(* com.sxit..*.*(..))")  

       (4),环绕通知

                ("execution(* com.sxit..*.*(..))") 

       (5),前置通知

               @Before("execution(* com.sxit..*.*(..))")  

    5,定义切入点

      @Pointcut("execution(* com.sxit..*.*(..))")   //针对具体要切入的方法进行说明,如果这块有说明,则切入方法@Before,@Around 都可不用再声明其具体的切入方法,直接声明pointcut声明的方法即可
       public void init(){  
        }  

    @Before(value="init()")  
            public void before(){  
                System.out.println("方法执行前执行.....");  
            }  

         

    AOP详解

    AOP核心概念

    1、横切关注点

    对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

    2、切面(aspect)

    类是对物体特征的抽象,切面就是对横切关注点的抽象

    3、连接点(joinpoint)

    被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

    4、切入点(pointcut)

    对连接点进行拦截的定义

    5、通知(advice)

    所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

    6、目标对象

    代理的目标对象

    7、织入(weave)

    将切面应用到目标对象并导致代理对象创建的过程

    8、引入(introduction)

    在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

  • 相关阅读:
    把影响集中到一个点
    How to avoid Over-fitting using Regularization?
    适定性问题
    Numerical Differentiation 数值微分
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    Generally a good method to avoid this is to randomly shuffle the data prior to each epoch of training.
    What is the difference between iterations and epochs in Convolution neural networks?
    Every norm is a convex function
    Moore-Penrose Matrix Inverse 摩尔-彭若斯广义逆 埃尔米特矩阵 Hermitian matrix
    perl 类里的函数调用其他类的函数
  • 原文地址:https://www.cnblogs.com/lodor/p/7716325.html
Copyright © 2011-2022 走看看