Spring BeanPostProcessor接口使用詳解
Spring中提供了很多PostProcessor供開發(fā)者進(jìn)行拓展,例如:BeanPostProcessor、BeanFactoryPostProcessor、BeanValidationPostProcessor等一系列后處理器。他們的使用方式大多類似,了解其中一個(gè)并掌握他的使用方式,其他的可以觸類旁通。
這里以BeanPostProcessor為例展示其使用方式。
BeanPostProcessor接口提供了兩個(gè)供開發(fā)者自定義的方法:postProcessBeforeInitialization、postProcessAfterInitialization。
postProcessBeforeInitialization:該方法主要針對(duì)spring在bean初始化時(shí)調(diào)用初始化方法前進(jìn)行自定義處理。
postProcessAfterInitialization:該方法主要針對(duì)spring在bean初始化時(shí)調(diào)用初始化方法后進(jìn)行自定義處理。
示例代碼:
/** * 測試bean */ public class Cat { private String name; private int age; public void say() { System.out.println("name:" + name); System.out.println("age:" + age); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
/** * 自定義后處理器 */ public class CatBeanPostProcessor implements BeanPostProcessor { @Nullable @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof Cat) { //輸出原始屬性 Cat cat = (Cat) bean; cat.say(); return bean; } return bean; } @Nullable @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof Cat) { //修改屬性值,并返回 Cat cat = (Cat) bean; cat.setName("hello maomi"); cat.setAge(3); return cat; } return bean; } }
/** * 運(yùn)行 */ public class Run { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml"); Cat cat = (Cat) applicationContext.getBean("cat"); cat.say(); } }
xml配置信息
<!--配置bean并初始化--> <bean id="cat" class="com.source.postprocessor.Cat" > <property name="name" value="HelloKitty" /> <property name="age" value="1" /> </bean> <bean id="catBeanPostProcessor" class="com.source.postprocessor.CatBeanPostProcessor" />
輸出結(jié)果:
name:HelloKitty
age:1
name:hello maomi
age:3
可以看到通過后處理器處理過后的bean信息已經(jīng)改變。最后,看看源碼中如何調(diào)用自定義實(shí)現(xiàn)的。
在初始化bean方法中:AbstractAutowireCapableBeanFactory.java
/** * 初始化bean */ protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) { //省略部分無關(guān)代碼 Object wrappedBean = bean; //初始化前 if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { //調(diào)用初始化方法初始化bean invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex); } //初始化后 if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; } //postProcessBeforeInitialization方法調(diào)用 @Override public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { //調(diào)用自定義postProcessBeforeInitialization方法 Object current = beanProcessor.postProcessBeforeInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; } //postProcessAfterInitialization方法調(diào)用 @Override public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { //自定義postProcessAfterInitialization方法調(diào)用 Object current = beanProcessor.postProcessAfterInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; }
以上就是spring對(duì)自定義方法實(shí)現(xiàn)的調(diào)用過程。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring中的后置處理器BeanPostProcessor詳解
- SpringBoot之通過BeanPostProcessor動(dòng)態(tài)注入ID生成器案例詳解
- Spring BeanPostProcessor(后置處理器)的用法
- Spring?BeanPostProcessor后處理器源碼解析
- 詳解使用Spring的BeanPostProcessor優(yōu)雅的實(shí)現(xiàn)工廠模式
- Spring探秘之如何妙用BeanPostProcessor
- Spring源碼解析之BeanPostProcessor知識(shí)總結(jié)
- Spring BeanPostProcessor源碼示例解析
- Spring注解驅(qū)動(dòng)之BeanPostProcessor后置處理器講解
- Spring組件初始化擴(kuò)展點(diǎn):BeanPostProcessor
相關(guān)文章
Java基于阻塞隊(duì)列實(shí)現(xiàn)生產(chǎn)者消費(fèi)者模型示例詳解
這篇文章主要介紹了Java基于阻塞隊(duì)列實(shí)現(xiàn)生產(chǎn)者消費(fèi)者模型,阻塞隊(duì)列的特點(diǎn)就是阻塞兩個(gè)字,阻塞功能使得生產(chǎn)者和消費(fèi)者兩端的能力得以平衡,當(dāng)有任何一端速度過快時(shí),阻塞隊(duì)列便會(huì)把過快的速度降下來,感興趣的朋友可以參考下2023-12-12idea2020.1無法自動(dòng)加載maven依賴的jar包問題及解決方法
這篇文章主要介紹了idea2020.1無法自動(dòng)加載maven依賴的jar包問題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07使用@Value 注入 List 類型的配置屬性需要注意的 BUG
這篇文章主要介紹了使用@Value 注入 List 類型的配置屬性需要注意的 BUG,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延時(shí)隊(duì)列的方案
在很多的業(yè)務(wù)場景中,延時(shí)隊(duì)列可以實(shí)現(xiàn)很多功能,此類業(yè)務(wù)中,一般上是非實(shí)時(shí)的,需要延遲處理的,需要進(jìn)行重試補(bǔ)償?shù)?本文給大家介紹了SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延遲隊(duì)列的方案,文中有詳細(xì)的代碼講解,需要的朋友可以參考下2024-04-04