zoukankan      html  css  js  c++  java
  • SpringAOP的两种实现方式

    1、SpringAOP,面向切面编程。在实际应用汇总很常见,一般用于日志、异常保存。也可以针对于相应的业务做处理

    AOP核心概念

      1、横切关注点

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

      2、切面(aspect)

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

      3、连接点(joinpoint)

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

      4、切入点(pointcut)

      对连接点进行拦截的定义

      5、通知(advice)

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

      6、目标对象

      代理的目标对象

      7、织入(weave)

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

      8、引入(introduction)

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

    2、切面的配置方式有两种:xml配置(建议)、注解

    3、第一种方式:注解

      1)在相应的包里面建好自己类

      2)在相关的类上面加入相关注解,目的是认为这个类是切面

        

      3)进行相关切面的处理。这里存在两种方式,一种是直接类的方式,一种是用注解的方式

      这是第一种方式,是直接对应相关的类里面的方法做处理

    package com.troy.ai.web.Interceptors;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class AOPTEST {
        @Pointcut("execution(* com.troy.ai.web.schedule.ScheduleController.*(..))")
        public void test(){}
        
        @Before("test()") //也可以@Before("execution(* com.troy.ai.web.schedule.ScheduleController.*(..))")
        public void before(JoinPoint joinPoint){
            System.out.println(joinPoint.getSignature().getName()+"before");
        }
        
        @After("test()")
        public void after(JoinPoint joinPoint){
            System.out.println(joinPoint.getSignature().getName()+"after");
        }
    }

      第二种:是通过注解的方式实现

      a、自己编写一个注解:

    import java.lang.annotation.*;
    
    @Target({ElementType.PARAMETER,ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface SystemLog {
        String  description()  default "";
    }

      b、再通过注解配置的方式来实现切面

    @Aspect
    @Component
    public class LogInterceptor {
        private long  startTime;
        private JSONObject json = new JSONObject();
        @Pointcut("@annotation(com.troy.ai.web.Interceptors.SystemLog)")  
        public void  logAspect(){}
        @Before("logAspect()")
        public void before(JoinPoint JoinPoint){}
    
        @AfterThrowing("logAspect()")  
        public void  afterThrowing(){}
    
        @After("logAspect()")
        public void  after(JoinPoint JoinPoint){}

      这样只需要在要切面的类的方法上面加入这个注解就可以了

      

      这样这个方法,就可以通过自己的逻辑实现相关业务

      3)解释:JoinPoint 是对应切面所提供的接口,可以通过JoinPoint 来查找具体的信息

      

    4、xml的配置方式,其实原理差不多

      1)还是写好对应的切面类(不需要做任何注解)

    import org.aspectj.lang.JoinPoint;
    public class AOPTEST {
    
        public void before(JoinPoint joinPoint){
            System.out.println(joinPoint.getSignature().getName()+"before");
        }
        
        public void after(JoinPoint joinPoint){
            System.out.println(joinPoint.getSignature().getName()+"after");
        }
    }

      2)进行xml配置

    <bean id="AOPTEST" class="com.troy.ai.web.Interceptors.AOPTEST"/>
        <aop:config>
            <aop:aspect id="aspect" ref="AOPTEST">
                <aop:pointcut expression="execution(* com.troy.ai.web.schedule.ScheduleController.*(..))" id="pointcut"/>
                <aop:before method="before" pointcut-ref="pointcut"/>
                <aop:after method="after" pointcut-ref="pointcut"/>
            </aop:aspect>
        </aop:config>

      3)这种方式也可以使用注解的方式来实现

    5、区别

      1)共同点:在效果过实现上面都是一样的,并且两种方式在引用过程中都很容易理解

      2)不同点:

        a、注解的方式方便代码书写,并且很容易针对于新增的方法做处理,但是不好的地方在于,注解很容易分散,不易管理

        b、xml的方式再是集中管理,不管业务逻辑关系在正在怎么变化,所有东西都统一配置,便于管理。相对的在使用上面就没有那么舒服了,在新增方法上面或者个别上面,会存在多配置的情况

  • 相关阅读:
    gridview 后台增加列
    SQL Server 触发器
    PIVOT 和 UNPIVOT 命令的SQL Server版本
    HtmlHelper.Raw,<%%>,<%:%>,<%=%>的区别及使用
    分布式搜索的面试题3
    分布式搜索的面试题2
    分布式搜索的面试题1
    消息队列的面试题7
    消息队列的面试题6
    消息队列的面试题5
  • 原文地址:https://www.cnblogs.com/ll409546297/p/6494738.html
Copyright © 2011-2022 走看看