zoukankan      html  css  js  c++  java
  • 第四章、AOP概述

    一、AOP概述

    1. AOP(Aspect-Oriented Programming,面向切面编程):是一种新的方法论,是对传 统 OOP(Object-Oriented Programming,面向对象编程)的补充。

    2. AOP实现的关键在于AOP框架自动创建的AOP代理,AOP代理主要分为静态代理和动态代理 以AspectJ为代表的静态代理。 以Spring AOP为代表的动态代理。

    3. AOP编程操作的主要对象是切面(aspect),而切面模块化横切关注点。

    4. 在应用AOP编程时,仍然需要定义公共功能,但可以明确的定义这个功能应用在哪里,以什么方式应用,并且不必修改受影响的类。这样一来横切关注点就被模块化到特殊的类里——这样的类我们通常称之为“切面”。

    5. AOP的好处:

      1. 每个事物逻辑位于一个位置,代码不分散,便于维护和升级

      2. 业务模块更简洁,只包含核心业务代码

      3. AOP图解

     

    二、AOP术语

    2.1、横切关注点

      从每个方法中抽取出来的同一类非核心业务(打印日志业务)。

    2.2 切面(Aspect)

      封装横切关注点信息的,每个关注点体现为一个通知方法(比如参数校验的类,打印日志的类)。

    2.3 通知(Advice)

      切面必须要完成的各个具体工作(参数校验的、打印日志的方法)

    2.4 目标(Target)

      被通知的对象

    2.5 代理(Proxy)

      向目标对象应用的

    2.6 连接点(Joinpoint)

      横切关注点在程序代码中的具体体现对应程序执行的某个特定位置。例如:类某个方法调用前、调用后、方法捕获到异常后等(比如日志代码的位置)。

      在应用程序中可以使用横纵两个坐标来定位一个具体的连接点:

    2.7 切入点(pointcut)

      定位连接点的方式。每个类的方法中都包含多个连接点,所以连接点是类中客观存在的事物。如果把连接点看作数据库中的记录,那么切入点就是查询条件——AOP可以通过切入点定位到特定的连接点。切点通过org.springframework.aop.Pointcut 接口进行描述,它使用类和方法作为连接点的查询条件。

    三、AspectJ

    AspectJ:Java社区里最完整最流行的AOP框架。

    在Spring2.0以上版本中,可以使用基于AspectJ注解或基于XML配置的AOP。

    对加减乘除的日志打印用AOP实现

    3.1、在Spring中启用AspectJ注解

         <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.2.2</version>
            </dependency>
    
            <dependency>
                <groupId>aopalliance</groupId>
                <artifactId>aopalliance</artifactId>
                <version>1.0</version>
            </dependency>
    
    
            <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.5</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>5.2.2.RELEASE</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>5.2.2.RELEASE</version>
            </dependency>
    • 引入aop名称空间:IDEA中配置文件中加<aop:aspectj-autoproxy/>自动引入需要的命名空间

    • 配置

      • <aop:aspectj-autoproxy>:当Spring IOC容器侦测到bean配置文件中的元素时,会自动为与AspectJ切面匹配的bean创建代理。

    <context:component-scan base-package="com.jdy.spring2020"/>
    <!--基于注解开发AspectJ:主要作用是为切面中能作用到的目标类生成代理-->
    <aop:aspectj-autoproxy/>

    3.2、用AspectJ注解声明切面

      上面讲到切面必须要完成的各个具体工作叫做通知,可以知道一个切面中可以有很多个通知。

      通知的分类

      • @Before:前置通知,在方法执行之前执行

      • @After:后置通知,在方法执行之后执行

      • @AfterRunning:返回通知,在方法返回结果之后执行

      • @AfterThrowing:异常通知,在方法抛出异常之后执行

      • @AfterThrowing:异常通知,在方法抛出异常之后执行

    /**
     * @Aspect:表示是一个切面
     */
    @Component
    @Aspect
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    public class LogAop {
    
        /**
         * 前置通知:
         * @Before("execution( com.jdy.spring2020.scan.service.impl.ArithmeticCalculatorImpl.add(int,int))")
         * 在ArithmeticCalculatorImpl.add方法前之前前置通知
         *
         */
        @Before(value = "execution(* com.jdy.spring2020.scan.service.impl.ArithmeticCalculatorImpl.add(int,int))")
        public void before(){
            System.out.println("AO....前置通知");
        }
    
        /**
         *
         * @param joinPoint:连接点对象
         */
        @AfterReturning(value = "execution(* com.jdy.spring2020.scan.service.impl.ArithmeticCalculatorImpl.add(int,int))",returning ="result" )
        public void after(JoinPoint joinPoint){
            //方法的名字
            String name = joinPoint.getSignature().getName();
            System.out.println("后置通知方法名字:" + name);
        }
    
        /**
         * 返回通知:目标方法执行结束后,得到方法的返回值
         * 获取方法的返回值:通过returnning来指定一个名字,必须要与方法的一个参数名成一致。
         * @param joinPoint
         * @param result
         */
        @AfterReturning(value = "execution(* com.jdy.spring2020.scan.service.impl.ArithmeticCalculatorImpl.add(int,int))",returning ="result" )
        public void afterRunning(JoinPoint joinPoint,Object result){
            //方法的名字
            String name = joinPoint.getSignature().getName();
            System.out.println("返回通知方法名字:" + name);
            System.out.println("返回通知方法返回值:" + result);
        }
    
    
       /**
         * 异常通知:目标方法执行结束后,目标方法抛出异常
         * 获取方法的异常:通过Throwing来指定一个名字,必须要与方法的一个参数名一致
         * 可以通过形参中异常的类型来设置抛出指定异常才会执行异常通知
         * @param joinPoint
         */
        @AfterThrowing(value = "execution(public int com.jdy.spring2020.scan.service.impl.ArithmeticCalculatorImpl.sub(int,int))",throwing = "ex")
        public void afterThrowing(JoinPoint joinPoint,Exception ex){
            //方法的名字
            String name = joinPoint.getSignature().getName();
            System.out.println("异常通知方法名字:" + name);
            System.out.println("异常通知方法返回异常:" + ex);
        }
    
        //可以通过形参中异常的类型来设置抛出指定异常才会执行异常通知
        @AfterThrowing(value = "execution(public int com.jdy.spring2020.scan.service.impl.ArithmeticCalculatorImpl.*(int,int))",throwing = "ex")
        public void afterThrowing1(JoinPoint joinPoint,NullPointerException ex){
            //方法的名字
            String name = joinPoint.getSignature().getName();
            System.out.println("异常通知方法名字" + name);
            System.out.println("异常通知方法返回异常" + ex);
        }
    
    
        /**
         * 环绕通知
         * @param joinPoint
         */
        @Around(value = "execution(public int com.jdy.spring2020.scan.service.impl.ArithmeticCalculatorImpl.add(int,int))")
        public Object around(ProceedingJoinPoint joinPoint) {
            //方法的名字
            Object obj = null;
            //前置
            try {
                //执行目标方法,相当于动态代理的invoke方法
                System.out.println("环绕通知---->方法名字" + joinPoint.getSignature().getName());
                obj = joinPoint.proceed();
                System.out.println("环绕通知---->方法proceed" + obj);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }finally {
                //后置
            }
            return obj;
        }
    }
    public interface ArithmeticCalculator {
        void add(int a,int b);
    }
    @Service("arithmeticCalculatorImpl")
    public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
        @Override
        public void add(int a, int b) {
            System.out.println("日志:The Method add begin ["+a+","+b+"]");
            int result = a+b;
            System.out.println("result = " + result);
        }
    }
    public class Test_03 {
        ClassPathXmlApplicationContext context = null;
        {
            context = new ClassPathXmlApplicationContext("application_03.xml");
        }
    
        @Test
        public void test() {
            ArithmeticCalculatorImpl impl = context.getBean("arithmeticCalculatorImpl", ArithmeticCalculatorImpl.class);
            impl.add(2,3);
        }
    }
  • 相关阅读:
    leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
    【jQuery 区别】.click()和$(document).on("click","指定的元素",function(){});的区别
    【前台 submit的重复提交 错误】submit的重复提交
    【spring 注解 错误】使用controller 作为后台给前台ajax交互数据出错
    【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能
    【Filter 页面重定向循环】写一个过滤器造成的页面重定向循环的问题
    【前台页面 BUG】回车按钮后,页面自动跳转
    【hibernate 执行方法未插入数据库】hibernate的save方法成功执行,但是未插入到数据库
    【maven 报错】maven项目执行maven install时报错Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
    【hibernate criteria】hibernate中criteria的完整用法 转
  • 原文地址:https://www.cnblogs.com/jdy1022/p/13678161.html
Copyright © 2011-2022 走看看