【Spring学习】之 AOP

来源:互联网 发布:社会学理论 知乎 编辑:程序博客网 时间:2024/04/29 23:03

一、AOP 的概述

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

  (2)为什么学习 AOP:

这里写图片描述

  (3)Spring 的 AOP 的由来:

这里写图片描述

  (4)底层实现:

这里写图片描述

JDK 动态代理增强一个类中方法:

public class MyJDKProxy implements InvocationHandler {    private UserDao userDao;    public MyJDKProxy(UserDao userDao) {        this.userDao = userDao;    }    // 编写工具方法:生成代理:    public UserDao createProxy(){    UserDao userDaoProxy = (UserDao)    Proxy.newProxyInstance(userDao.getClass().getClassLoader(),    userDao.getClass().getInterfaces(), this);    return userDaoProxy;    }    @Override    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable    {        if("save".equals(method.getName())){            System.out.println("权限校验================");        }        return method.invoke(userDao, args);    }}

Cglib 动态代理增强一个类中的方法:

public class MyCglibProxy implements MethodInterceptor{    private CustomerDao customerDao;    public MyCglibProxy(CustomerDao customerDao){        this.customerDao = customerDao;    }    // 生成代理的方法:    public CustomerDao createProxy(){        // 创建 Cglib 的核心类:        Enhancer enhancer = new Enhancer();        // 设置父类:        enhancer.setSuperclass(CustomerDao.class);        // 设置回调:        enhancer.setCallback(this);        // 生成代理:        CustomerDao customerDaoProxy = (CustomerDao) enhancer.create();        return customerDaoProxy;    }    @Override    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy    methodProxy) throws Throwable {        if("delete".equals(method.getName())){            Object obj = methodProxy.invokeSuper(proxy, args);            System.out.println("日志记录================");            return obj;        }        return methodProxy.invokeSuper(proxy, args);    }}



三、AOP 的开发中的相关术语

这里写图片描述
这里写图片描述



四、AOP操作

(一)XML操作

这里写图片描述

导包

这里写图片描述

ApplicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --></beans>

表达式

这里写图片描述

Book.java

这里写图片描述

MyBook.java

这里写图片描述

测试类和结果

这里写图片描述



(二)注解操作

这里写图片描述
这里写图片描述

applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">    <!-- 开启 aop 注解的自动代理: -->    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>    <!-- 1  配置对象 -->    <bean id="book" class="cn.yyf.aop.Book"></bean>    <bean id="myBook" class="cn.yyf.aop.MyBook"></bean></beans>

这里写图片描述
这里写图片描述

Book.java

这里写图片描述

MyBook.java

这里写图片描述

测试类以及结果

这里写图片描述
这里写图片描述

1 0