spring06


title: spring06
date: 2020-03-09 19:31:51
tags:该部分记录了AOP


记录了AOP,有方法执行前,方法执行后,方法环绕。

1、概述

**摘自秦老师... **

什么是AOP?

AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

AOP在Spring中的作用:

提供声明式事务;允许用户自定义切面

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 ....
  • 切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
  • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
  • 目标(Target):被通知对象。
  • 代理(Proxy):向目标对象应用通知之后创建的对象。
  • 切入点(PointCut):切面通知 执行的 “地点”的定义。
  • 连接点(JointPoint):与切入点匹配的执行点。

2、练习的环境

  • 接口(UserService)

    package com.nevesettle.service;
    
    public interface UserService {
        public void add();
        public void delete();
        public void update();
        public void search();
    
    }
    
  • 实现类(UserServiceImp)

    package com.nevesettle.service;
    
    public class UserServiceImp implements UserService {
        public void add() {
            System.out.println("执行了ADD方法");
        }
    
        public void delete() {
            System.out.println("执行了DELETE方法");
        }
    
        public void update() {
            System.out.println("执行了UPDATE方法");
        }
    
        public void search() {
            System.out.println("执行了SEARCH方法");
        }
    }
    

3、实现的方式一

1、所需类

定义两个需要执行的函数:

  • 方法执行前函数(beforeLog)

    package com.nevesettle.log;
    
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    import java.lang.reflect.Method;
    
    public class BeforeLog implements MethodBeforeAdvice {
    
        //method : 要执行的目标对象的方法
        //objects : 被调用的方法的参数
        //Object : 目标对象
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println(o.getClass().getName() + "类执行了" + method.getName() + "方法!");
        }
    }
    
  • 方法执行后函数(afterLog)

    package com.nevesettle.log;
    
    
    import org.springframework.aop.AfterReturningAdvice;
    
    import java.lang.reflect.Method;
    
    public class AfterLog implements AfterReturningAdvice {
    
    
        //returnValue 返回值
        //method被调用的方法
        //args 被调用的方法的对象的参数
        //target 被调用的目标对象
        public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
            System.out.println(method.getName() + "执行后,返回了" + o);
        }
    }
    
    
    • AfterReturningAdvice 该类是有返回参数的方法
    • 类都要实现相应的接口,如在切入点之前执行就要继承 MethodBeforeAdvice ,然后实现相应的逻辑代码

2、xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           https://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/aop
                           https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--    配置bean-->
    <bean id="serviceImp" class="com.nevesettle.service.UserServiceImp"/>
    <bean id="beforeLog" class="com.nevesettle.log.BeforeLog"/>
    <bean id="afterLog" class="com.nevesettle.log.AfterLog"/>

    <!--第一种方式,配置AOP-->
    	<aop:config>
        <!--        切入点,execution是固定的表达式-->
        <aop:pointcut id="myPointcut" expression="execution(* com.nevesettle.service.UserServiceImp.*(..))"/>

        <!--        执行环绕-->
        <aop:advisor advice-ref="afterLog" pointcut-ref="myPointcut"/>
        <aop:advisor advice-ref="beforeLog" pointcut-ref="myPointcut"/>
    </aop:config>



</beans>
  • aop:pointcut 的意思是设置一个切入点,也就是在那里增加方法。
  • execution(* com.nevesettle.service.UserServiceImp.*(..)) 式子的意思是可以接受一切返回值
    • 切入的位置是com.nevesettle.service包下的UserServiceImp类的所有方法
    • (..)代表可以接受一切参数
    • 基本格式就是 包路径.类名.方法名(..)

4、实现的方式二

只需要一个类即可,该类中有要执行的各个方法。

package com.nevesettle.diy;

public class DiyLog {
    public void after(){
        System.out.println("=======方法执行后=======");
    }
    public void before(){
        System.out.println("=======方法执行前=======");
    }
}

然后再在xml中配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

<!--    配置bean-->
    <bean id="serviceImp" class="com.nevesettle.service.UserServiceImp"/>
    <bean id="beforeLog" class="com.nevesettle.log.BeforeLog"/>
    <bean id="afterLog" class="com.nevesettle.log.AfterLog"/>

<!--    第二种方式-->
    <bean id="diylog" class="com.nevesettle.diy.DiyLog"/>
<!--    开始配置AOP-->
    <aop:config>
        <aop:aspect ref="diylog">
<!--            切入点-->
            <aop:pointcut id="myPointcut" expression="execution(* com.nevesettle.service.UserServiceImp.*(..))"/>
<!--            通知-->
            <aop:after method="after" pointcut-ref="myPointcut"/>
            <aop:before method="before" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>

</beans>
  • 首先要注册该自定义的log类
  • 可以在AOP的config中配置切入点和自定义的log类的方法执行位置
原文地址:https://www.cnblogs.com/wuren-best/p/12450765.html