【Spring学习】之 AOP

一、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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值