java基础-day52-Spring02

2021-07-11  本文已影响0人  触手不可及

今日内容--

spring

Spring中的AOP

什么是AOP
AOP Aspect Oriented Programing 面向切面编程
AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视、事务管理、安全检查、缓存)
SpringAOP使用纯Java实现,不需要专门的编译过程和类加载器,在运行期通过代理方式向目标类织入增强代码

AOP底层原理:就是代理机制

动态代理:
特点:字节码随用随创建,随用随加载
作用:不修改源码的基础上对方法增强
分类:
基于接口的动态代理
基于子类的动态代理

Spring的AOP代理:

JDK动态代理:被代理对象必须要实现接口,才能产生代理对象.如果没有接口将不能使用动态代理技术。
CGLib动态代理:第三方代理技术,cglib代理.可以对任何类生成代理.代理的原理是对目标对象进行继承代理. 如果目标对象被final修饰.那么该类无法被cglib代理.

结论:Spring框架,如果类实现了接口,就使用JDK的动态代理生成代理对象,如果这个类没有实现任何接口,使用CGLIB生成代理对象。

AOP的术语:

Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点.
Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义.
Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.
Target(目标对象):代理的目标对象
Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程.
spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入
Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类
Aspect(切面): 是切入点和通知(引介)的结合
AOP案例:

1.在pom.xml添加aop依赖
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
    </dependency>

2.创建通知类
    前置通知(before):目标方法运行之前调用
    后置通知(after-returning):在目标方法运行之后调用 (如果出现异常不会调用)
    环绕通知(around):在目标方法之前和之后都调用(ProceedingJoinPoint对象 -->> 调用proceed方法)
    异常拦截通知(after-throwing):如果出现异常,就会调用
    最终通知(after):在目标方法运行之后调用 (无论是否出现 异常都会调用)
3.创建applicationContext.xml,添加aop约束

    <aop:config>
        <!-- 配置切入点 切入点表达式的写法:execution(表达式)
            public void com.abyg.service.UserServiceImpl.save() 
            void com.qf.service.UserServiceImpl.save()  其他修饰符无返回值的save空参方法
            * com.qf.service.UserServiceImpl.save()  有或者无返回值的save空参方法
            * com.qf.service.UserServiceImpl.*()  有或者无返回值的所有空参方法
            
            * com.qf.service.*ServiceImpl.*(..)  有或者无返回值的所有有参或者空参方法
            * com.qf.service..*ServiceImpl.*(..)  一般不用,service包下的子包和孙包以ServiceImpl结尾的类中的方法
        -->
        <aop:pointcut expression="execution(* com.qf.service.*ServiceImpl.*(..))" id="pc"/>
        <aop:aspect ref="myAdvice" >
            <!-- 指定名为before方法作为前置通知 -->
            <aop:before method="before" pointcut-ref="pc" />
            <!-- 后置 -->
            <aop:after-returning method="afterReturning" pointcut-ref="pc" />
            <!-- 环绕通知 -->
            <aop:around method="around" pointcut-ref="pc" />
            <!-- 异常拦截通知 -->
            <aop:after-throwing method="afterException" pointcut-ref="pc"/>
            <!-- 后置 -->
            <aop:after method="after" pointcut-ref="pc"/>
        </aop:aspect>
    </aop:config>
Spring中的注解配置AOP(了解会用)

//通知类
@Aspect
//表示该类是一个通知类
public class MyAdvice {
    //自己设置一个切点,管理重复代码
    @Pointcut("execution(* com.qf.service.*ServiceImpl.*(..))")
    public void pc(){}
    //前置通知
    //指定该方法是前置通知,并制定切入点
    @Before("MyAdvice.pc()")
    public void before(){
        System.out.println("这是前置通知!!");
    }
    //后置通知
    @AfterReturning("execution(* com.qf.service.*ServiceImpl.*(..))")
    public void afterReturning(){
        System.out.println("这是后置通知(如果出现异常不会调用)!!");
    }
    //环绕通知
    @Around("execution(* com.qf.service.*ServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知之前的部分!!");
        Object proceed = pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分!!");
        return proceed;
    }
    //异常通知
    @AfterThrowing("execution(* com.qf.service.*ServiceImpl.*(..))")
    public void afterException(){
        System.out.println("出事啦!出现异常了!!");
    }
    //后置通知
    @After("execution(* com.qf.service.*ServiceImpl.*(..))")
    public void after(){
        System.out.println("这是后置通知(出现异常也会调用)!!");
    }
}
applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
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-4.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.2.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

<!-- 准备工作: 导入aop(约束)命名空间 -->
    <!-- 1.配置目标对象 -->
    <bean name="userService" class="com.qf.service.UserServiceImpl" ></bean>
    <!-- 2.配置通知对象 -->
    <bean name="myAdvice" class="com.qf.annotation_aop.MyAdvice" ></bean>
    <!-- 3.开启使用注解完成织入 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>
上一篇下一篇

猜你喜欢

热点阅读