网站建设合同要不要交印花税,个人网站盈利,广告设计公司取名,wordpress搭建购物网站Spring后处理器-BeanPostProcessor
Bean被实例化后#xff0c;到最终缓存到名为singletonObjects单例池之前#xff0c;中间会经过bean的初始化过程#xff08;#xff08;该后处理器的执行时机#xff09;#xff09;#xff0c;例如#xff1a;属性的填充、初始化方…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 version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:utilhttp://www.springframework.org/schema/utilxmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://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.xsdbean classcom.example.PostProcessor.MyBeanPostProcessor/beanbean iduserService classcom.example.Service.Impl.UserServiceImpl init-methodinit
/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