家政公司网站建设方案/广州网站seo地址
Spring后处理器-BeanPostProcessor
- Bean被实例化后,到最终缓存到名为singletonObjects单例池之前,中间会经过bean的初始化过程((该后处理器的执行时机)),例如:属性的填充、初始化方法init的执行等,其中有一个对外拓展的点BeanPostProcessor,我们称之为bean后处理器。与上文bean工厂后处理器相似,它也是一个接口,实现了该接口并被容器管理的BeanPostProcessor(即在配置文件中对其进行配置),会在流程节点上被Spring自动调用。
- BeanPostProcessor接口代码如下
-
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) //package org.springframework.beans.factory.config;import org.springframework.beans.BeansException; import org.springframework.lang.Nullable;public interface BeanPostProcessor {@Nullabledefault Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}@Nullabledefault Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;} }
-
- 创建实现该接口(BeanPsotProcessor)的类,要在配置文件中进行管理
- 快捷键 ctrl + insert 重写接口方法
-
package com.example.PostProcessor;import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println(beanName + ":postProcessBeforeInitialization");return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println(beanName + ":postProcessAfterInitialization");return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);} }
-
测试类代码
-
package com.example.Test;import com.example.Service.Impl.UserServiceImpl; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApplicationContext {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");System.out.println(context.getBean(UserServiceImpl.class));} }
-
-
运行结果如下
-
-
展示了该后处理器的执行时机
-
Before和After执行时机
- 在Bean实例化过程可以配置相关对于bean对象的操作方法,具体间往期文章:Bean的配置- CSDN搜索
- 注册为bean的类
-
package com.example.Service.Impl;import com.example.DAO.UserDAO; import com.example.Service.UserService; import org.springframework.beans.factory.InitializingBean;import java.util.List; import java.util.Map; import java.util.Set;public class UserServiceImpl implements UserService, InitializingBean {// todo 无参构造方法public UserServiceImpl() {System.out.println("UserServiceImpl实例化");}// todo 自定义初始化方法public void init() {System.out.println("自定义初始化方法init()");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("属性设置之后执行afterPropertiesSet()");}}
-
- 实现bean后处理器的类
-
package com.example.PostProcessor;import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println(beanName + ":postProcessBeforeInitialization");return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println(beanName + ":postProcessAfterInitialization");return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);} }
-
-
配置文件
-
<?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:util="http://www.springframework.org/schema/util"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><bean class="com.example.PostProcessor.MyBeanPostProcessor"></bean><bean id="userService" class="com.example.Service.Impl.UserServiceImpl" init-method="init"> </beans>
-
-
测试类
-
package com.example.Test;import com.example.Service.Impl.UserServiceImpl; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApplicationContext {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");System.out.println(context.getBean(UserServiceImpl.class));} }
-
-
运行结果
-
小结
- 从上述运行结果来看,首先完成bean对象的创建,然后执行后处理器中的before方法,然后执行属性设置之后的方法,然后执行自定义的初始化方法,最后执行后处理器的after方法
案例
- 对Bean方法进行执行时间日志增强
- 要求
- Bean的方法执行之前控制台打印当前时间
- Bean的方法执行之后控制台打印当前时间
- 分析
- 对方法进行增强主要就是代理设计模式和包装设计模式
- 由于Bean方法不确定,所以使用动态代理在运行期间执行增强操作
- 在Bean实例创建完毕之后,进入到单例之前,使用Proxy真实的目标bean