【Spring学习】之 AOP

    xiaoxiao2021-03-26  22

    一、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

    测试类以及结果

    转载请注明原文地址: https://ju.6miu.com/read-659614.html

    最新回复(0)