AOP面向切面

一、代理设计模式

代理设计模式:实现核心业务和辅助功能的分离

在这里插入图片描述

1.1 静态代理

代理类只能为特定的类做代理

1.2 动态代理

代理类几乎可以为所有的类进行代理

JDK动态代理类

只能为实现接口的类产生代理对象

  • 创建JDK动态代理类
public class JDKDynamicProxy implements InvocationHandler {

    //被代理对象
    private Object obj;
    public JDKDynamicProxy(Object obj){
        this.obj = obj;
    }

    //产生代理对象
    public Object getProxy(){
        ClassLoader classLoader = obj.getClass().getClassLoader();
        Class<?>[] interfaces = obj.getClass().getInterfaces();
        return Proxy.newProxyInstance(classLoader,interfaces,this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object v = method.invoke(obj, args);
        after();
        return v;
    }

    private void before(){
        System.out.println("~~~~~开启事务");
    }
    private void after(){
        System.out.println("~~~~~提交事务");
    }

}
  • 使用JDK动态代理类
IDAO proxyObj = (IDAO) new JDKDynamicProxy(new UserDAO()).getProxy();  

//无论使用代理对象调用任何方法 实际上都是去执行  invoke(Object proxy, Method method, Object[] args)
proxyObj.delete();  
CGLib动态代理

CGLib动态代理是通过产生被代理类的子类来产生代理对象的

因此:CGLib不能为final修饰的类产生代理对象

  • 创建CGLib动态代理(需要添加CGLib的依赖)
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.3.0</version>
</dependency>
public class CGLibDynamicProxy implements MethodInterceptor {

    private Object obj;
    public CGLibDynamicProxy(Object obj){
        this.obj = obj;
    }

    //CGLib动态代理是通过产生被代理类的子类来产生代理对象的
    //因此:CGLib不能为final修饰的类产生代理对象
    public Object getProxy(){
        Enhancer en = new Enhancer();
        en.setSuperclass(obj.getClass());
        en.setCallback(this);
        return en.create();
    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("=====beign");
        Object v = method.invoke(obj, objects);
        System.out.println("=====commit");
        return v;
    }
}
  • 使用CGLib动态代理类
UserDAO userDAO = (UserDAO) new CGLibDynamicProxy(new UserDAO()).getProxy();
userDAO.delete();

二、面向切面编程

动态代理能够在不改变原有业务逻辑的情况下能够实现对原有业务的增强

2.1 AOP概念

AOP (Aspect Oriented Programming) 面向切面编程 ,利用一种“横切”技术,对原有的业务逻辑进行拦截,并且在这个横切面上添加特定的业务逻辑

2.2 相关术语

连接点(Joinpoint):程序的业务流中客观存在的方法

切入点(Pointcut):被Spring横切的连接点

通知、增强:配置增强的业务的切入方式(before\after\around)

切点:添加到切入点的增强业务的方法

切面:定义切点方法的类

在这里插入图片描述

2.3 AOP开发流程
2.3.1 创建Maven项目

2.3.2 添加依赖
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.2.7.RELEASE</version>
</dependency>
2.3.3 创建配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>
2.3.4 AOP使用
  • 创建切面类,定义切点方法

    public class TxManager {
    
        public void begin(){
            System.out.println("****begin");
        }
        public void commit(){
            System.out.println("****commit");
        }
    
    }
    
  • 在applicationContext.xml文件中 配置切面类 (在配置文件添加aop的命名空间)

    <?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 id="bookDAO" class="com.qfedu.dao.BookDAO"></bean>
    
        <bean id="bookService" class="com.qfedu.service.BookService">
            <property name="bookDAO" ref="bookDAO"/>
        </bean>
    
        <bean id="txManager" class="com.qfedu.util.TxManager"></bean>
    
        <aop:config>
            <!--定义切入点-->
            <aop:pointcut id="bookDao_insert" expression="execution(* com.qfedu.dao.BookDAO.insert())"/>
    
            <!--声明切面类-->
            <aop:aspect ref="txManager">
                <!--通知切面类中切点方法 切入到 指定 切入点的 通知策略-->
                <aop:before method="begin" pointcut-ref="bookDao_insert"/>
                <aop:after method="commit" pointcut-ref="bookDao_insert"/>
            </aop:aspect>
            
        </aop:config>
    
    </beans>
    
2.4 切入点的定义

被Spring“横切”的连接点

  • 切入点声明语法
<!--
        使用 aop:pointcut 定义切入点
        id属性:切入点的唯一标识
        expression属性: 用于定义切入点

        说明:一个pointcut的声明可以一个方法,也可以标识多个方法
-->
<aop:pointcut id="pc1" expression="execution(* com.qfedu.dao.BookDAO.insert())"/>
<aop:pointcut id="pc2" expression="execution(* com.qfedu.dao.*.insert())"/>
<aop:pointcut id="pc3" expression="execution(* com.qfedu.dao.*.*())"/>
<aop:pointcut id="pc4" expression="execution(* com.qfedu.dao.*.*(..))"/>
<aop:pointcut id="pc5" expression="execution(* *(..))"/>
2.5 通知策略
  • 环绕策略连接点方法定义:
public class TimeManager {

    /**
     * 环绕策略的切点方法:
     */
    public Object getTime(ProceedingJoinPoint point) throws Throwable {
        long time1 = System.currentTimeMillis();
        Object v = point.proceed();
        long time2 = System.currentTimeMillis();
        System.out.println("------执行时间:"+(time2-time1));
        return v;
    }

}
  • 配置
<!--声明切面类-->
<aop:aspect ref="txManager">
    <!--通知切面类中切点方法 切入到 指定 切入点的
            通知策略
            before
            after
            around
            after-returning 切点方法在 切入点方法返回之后执行
            aop:after-throwing: 切点方法在 切入点方法抛出异常 之后执行
            -->
    <aop:before method="begin" pointcut-ref="pc2"/>
    <aop:after method="commit" pointcut-ref="pc2"/>
</aop:aspect>

<aop:aspect ref="timeManager">
    <aop:around method="getTime" pointcut-ref="pc2"/>
</aop:aspect>

eManager {

/**
 * 环绕策略的切点方法:
 */
public Object getTime(ProceedingJoinPoint point) throws Throwable {
    long time1 = System.currentTimeMillis();
    Object v = point.proceed();
    long time2 = System.currentTimeMillis();
    System.out.println("------执行时间:"+(time2-time1));
    return v;
}

}


- 配置

```xml
<!--声明切面类-->
<aop:aspect ref="txManager">
    <!--通知切面类中切点方法 切入到 指定 切入点的
            通知策略
            before
            after
            around
            after-returning 切点方法在 切入点方法返回之后执行
            aop:after-throwing: 切点方法在 切入点方法抛出异常 之后执行
            -->
    <aop:before method="begin" pointcut-ref="pc2"/>
    <aop:after method="commit" pointcut-ref="pc2"/>
</aop:aspect>

<aop:aspect ref="timeManager">
    <aop:around method="getTime" pointcut-ref="pc2"/>
</aop:aspect>
2.6 AOP实现原理

如果一个类被定义成了切入点,那么Spring容器在创建这个类的实例之后,会为之创建代理对象;存储在Spring对象工厂的是这个代理对象

动态代理的选择:
如果实现了接口就使用JDK动态代理,如果没有实现接口就使用CGLib动态代理

后处理器

Spring对象工厂在完成对象的创建之后,需要对对象进行再加工;
Spring提供了多个后处理器,对创建好的对象进行不同的处理,例如:AspectjAwareAdvisorAutoProxyCreator用于完成切入点类代理对象的创建
我们也可以自定义后处理器,在对象创建完成之后进行自定义处理

三、AOP注解配置

配置aop自动代理
<?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:context="http://www.springframework.org/schema/context"
       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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.qfedu"/>
    <aop:aspectj-autoproxy/>

</beans>
AOP注解
@Component
@Aspect
public class TxManager {

    //定义切入点
    @Pointcut("execution( * com.qfedu.dao.*.*(..))")
    public void pc(){}

    //通知策略 Before
    @Before("pc()")
    public void begin(){
        System.out.println("begin tx...");
    }

    @After("pc()")
    public void commit(){
        System.out.println("commit tx...");
    }

    @Around("execution( * com.qfedu.dao.*.*(..))")
    public Object getTime(ProceedingJoinPoint point) throws Throwable {
        System.out.println("time1:"+System.currentTimeMillis());
        Object v = point.proceed();
        System.out.println("time2:"+System.currentTimeMillis());
        return v;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值