javascript
Spring系列(八):Spring生命周期中BeanPostProcessor接口用法介绍
今天給大家介紹BeanPostProcessor接口用法,希望對大家能有所幫助!
? ? ? ? ? ? ? ?
1、BeanPostProcessor 概念介紹
BeanPostProcessor接口通常被稱為Bean的后置處理器,它是Spring中定義的接口,可以在Spring容器的創(chuàng)建過程中(主要在Bean初始化前后進行工作)回調(diào)BeanPostProcessor中定義的兩個方法。
2、BeanPostProcessor接口方法
postProcessBeforeInitialization:在每一個bean對象的初始化方法調(diào)用之前回調(diào)。
postProcessAfterInitialization:在每個bean對象的初始化方法調(diào)用之后被回調(diào)。
說明:以上兩個方法的返回值都不能為null,否則在后續(xù)的初始化方法會報空指針或者通過getBean()方法無法獲取Bean對象。主要原因是如果返回值為null的情況下以上兩個方法從Spring容器獲取bean實例,沒有再次放入Spring容器中去,這樣容器中就沒有了。
2.1 BeanPostProcessor源碼內(nèi)容
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;} }3、BeanPostProcessor 接口的作用
可以Spring容器中完成bean實例化、配置以及其他初始化方法前后根據(jù)業(yè)務的場景增加相應的邏輯處理。典型的案例AOP的實現(xiàn)。
4、代碼示例
4.1 新建Person.java 類文件
package com.spring.bean;public class Person {private String name;private Integer age;private String address;public Person(String name, Integer age, String address) {this.name = name;this.age = age;this.address = address;}public Person() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age='" + age + '\'' +", address='" + address + '\'' +'}';} }4.2、新建MyBeanPostProcessor.java
package com.spring.bean;import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component;@Component public class MyBeanPostProcessor implements BeanPostProcessor {/*** 實例化、依賴注入完畢,在調(diào)用顯示的初始化之前完成一些自定義的初始化邏輯* 方法返回值不能為null* 返回null那么在后續(xù)初始化方法會報空指針異常或者通過getBean()方法獲取不到bena實例* 如果設置為null情況下,后置處理器從Spring IoC容器中取出bean實例對象沒有再次放回IoC容器中*/public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("postProcessBeforeInitialization執(zhí)行了"+beanName);return bean;}/*** 實例化、依賴注入、初始化完成后時執(zhí)行* 方法返回值也不能為null* 如果返回null那么在后續(xù)初始化方法將報空指針異常或者通過getBean()方法獲取不到bena實例對象* 如果設置為null情況下,后置處理器從Spring IoC容器中取出bean實例對象沒有再次放回IoC容器中*/public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("postProcessAfterInitialization"+beanName);return bean;} }4.3、新建TestBeanPostProcessorConfig.java 配置類
package com.spring.config;import com.spring.bean.UserInfo; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller;@Configuration @ComponentScan(value = "com.spring.bean",includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Component.class, Controller.class}),@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {UserInfo.class}), }) public class TestBeanPostProcessorConfig {}4.4、新建測試類 TestBeanPostProcessor.java
package com.spring.test;import com.spring.config.TestBeanPostProcessorConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestBeanPostProcessor {public static void main(String[] args) {AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(TestBeanPostProcessorConfig.class);} }輸出結(jié)果:postProcessBeforeInitialization執(zhí)行了testBeanPostProcessorConfig postProcessAfterInitializationtestBeanPostProcessorConfig UserInfo構(gòu)造器執(zhí)行了 postProcessBeforeInitialization執(zhí)行了userInfo PostConstruct 初始化方法執(zhí)行 postProcessAfterInitializationuserInfoProcess finished with exit code 0IT技術(shù)分享社區(qū)
個人博客網(wǎng)站:https://programmerblog.xyz
文章推薦程序員效率:畫流程圖常用的工具程序員效率:整理常用的在線筆記軟件遠程辦公:常用的遠程協(xié)助軟件,你都知道嗎?51單片機程序下載、ISP及串口基礎(chǔ)知識硬件:斷路器、接觸器、繼電器基礎(chǔ)知識
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的Spring系列(八):Spring生命周期中BeanPostProcessor接口用法介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样运行python_怎样运行pytho
- 下一篇: 100个网络基础知识,赶紧收藏吧!