Spring(第七根)

104 阅读3分钟

Bean后处理器

  • 对容器的所有Bean都进行某固定化的增强的修改,使得容器中所以Bean都增加某功能

如何实现容器后处理

  • 提供一个实现BeanPostProcesssor接口的类 该类就是-一个Bean后处理器

    • object postProcessAfterIni tialization(Object bean, String beanName);初始化之后的后处理。
    • 0bject postProcessBeforeIni tialization(Object bean, String beanName): 初始化之前的后处理
    • 这个方法有2个参数 bean;该参数代表了将要接受后处理的Bean。(原始的) beanNamet将要接受处理的Bean的配置id 返回值,表示已经处理结束的Bean
  • 需要将Bean后处理器注册在容器中。 如果使用ApplicactionContext作为Spring容器,只要将Bean后处理器配置在Spring容器中即可。 如果使用BeanF actory作为Spring容器, 需要调用BeanFactory的方法来手动注册。

AOP

AOP实现分类

  • 静态AOP实现: AOP框架在编译阶段即实现对目标类的增强(就是对見标类的class文件进行修改)↓生成静态的AOP代理类 (生成*. class文件已经被改掉了,需要使动态AOP实现: AOP框架在运行阶段动态生成AOP代理”(在内存中动态地生成AOP代理类) ,以实现对目标对象的增强。以Spring AOP为代表。

  • 动态A0P实现: AOP框架 在运行阶段动态生成AOP代理(在内存中动态地生&OP代理类) , 以实现对目标对象的增强。以Spring AOP为代表。

AOP的基本概念

  • 切面(Aspect) :看上去像类,但明显不是类的一-种特殊的东西。Aspect用于包含多个Advice 增强处理(Advice: 通知、建议) : AOP框架在特定的切入点插入的增强处理。处理有“around"、before”和“after”等类型。Advice需要定义在Aspect里面。
  • 连接点(Joinpoint) :程序执行过程中明确的点,如方法的调用,或者异常的抛出。 Spring AOP中,连接点总是方法的调用。
  • 切入点(Pointcut). :可以插入增强处理的连接点。 简而言之,当某个连接点满足指定要求时,该连接点将被添加增强处理,该连接点也就变成了切入点。
  • 引入:添加方法或字段(field)到被处理的类。 Spring允许引入新的接口到任何被处理的对象。例如,你可以使用引入, 使任何对象实现IsModi fi ed接口 引入:是对目标类- -种修改方式,包括添加方法、添加成员变量、添加实现的接口
  • 目标对象:被AOP框架进行增强处理的对象,也被称为被增强的对象。 如果AOP框架是通过运行时代理来实现的,那这个对象将是一一个被代理的对象。 AOP代理: AOP框架创建的对象。简单地说,代理就是对目标对象的加强。 Spring中的AOP代理可以是JDK动态代理(需要实现接口),也可以是CGLIB代理。 前者为实现接口的目标对象的代理,后者为不实现接口的目标对象的代理。
  • 织入(Weaving). :将增强处理添加到目标对象中、并创建-个被增强的对象(AOP代理)的过程就是织入。织入有两种实现 方式:编译时增强(例如AspectJ)和运行时增强(例如CGLIB)。Spring和其他纯Java AOP框架一 样,在运行时完成织入。 织入:是对目标类- -种修改方式, 将Advice添加到目标对象的目标方法中的过程,就叫织入。

image.png

//在xml的<beans><beans/>中引入
xmlns:p="http://www.springframework.org/schema/p"  
       xmlns:c="http://www.springframework.org/schema/c"  
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:aop="http://www.springframework.org/schema/aop"  
       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       http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop.xsd       http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd"
<bean id="auth" class="com.example.ssm.day9.AuthAspect"/>  
<!--aop 配置的根元素-->  
<aop:config>  
    <aop:aspect ref="auth">  
        <!--把Bean转换位aspect-->  
        <aop:before method="auth" pointcut="execution(* com.example.ssm.day9.*.*(..))"/>  
    </aop:aspect>  
</aop:config>

通过pointcut属性指定将Advice织入目标方法的哪个位置

public class SpringTest {  
    public static void main(String[] args) {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("Bean9.xml");  
        System.out.println("==============");  
        Person person =(Person) ctx.getBean("person");  
        System.out.println(person.toString());  
    }  
}

image.png 当你使用AspectJ注解来驱动A0P时,需要添加如下元素来驱动它们。

image.png