解析Java的Spring框架的BeanPostProcessor發(fā)布處理器
BeanPostProcessor 的接口定義,可以實(shí)現(xiàn)提供自己的實(shí)例化邏輯,依賴解析邏輯等,也可以以后在Spring容器實(shí)例化完畢,配置和初始化一個(gè)bean通過插入一個(gè)或多個(gè)的BeanPostProcessor實(shí)現(xiàn)一些自定義邏輯回調(diào)方法實(shí)現(xiàn)。
可以配置多個(gè)的BeanPostProcessor接口,控制這些的BeanPostProcessor接口,通過設(shè)置屬性順序執(zhí)行順序提供的BeanPostProcessor實(shí)現(xiàn)了Ordered接口。
BeanPostProcessor可以對(duì)bean(或?qū)ο螅┎僮鲗?shí)例,這意味著Spring IoC容器實(shí)例化一個(gè)bean實(shí)例,然后BeanPostProcessor的接口做好自己的工作。
ApplicationContext會(huì)自動(dòng)檢測(cè)已定義實(shí)現(xiàn)的BeanPostProcessor接口和注冊(cè)這些bean類為后置處理器,可然后通過在容器創(chuàng)建bean,在適當(dāng)時(shí)候調(diào)用任何bean。
示例:
下面的示例顯示如何編寫,注冊(cè)和使用BeanPostProcessor 可以在一個(gè)ApplicationContext 的上下文。
使用Eclipse IDE,然后按照下面的步驟來創(chuàng)建一個(gè)Spring應(yīng)用程序:

這里是 HelloWorld.java 文件的內(nèi)容:
package com.yiibai;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
public void init(){
System.out.println("Bean is going through init.");
}
public void destroy(){
System.out.println("Bean will destroy now.");
}
}
這是實(shí)現(xiàn)BeanPostProcessor,之前和之后的任何bean的初始化它打印一個(gè)bean的名字非常簡(jiǎn)單的例子??梢砸?yàn)橛袃蓚€(gè)后處理器的方法對(duì)內(nèi)部bean對(duì)象訪問之前和實(shí)例化一個(gè)bean后執(zhí)行更復(fù)雜的邏輯。
這里是InitHelloWorld.java文件的內(nèi)容:
package com.yiibai;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;
public class InitHelloWorld implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean,
String beanName) throws BeansException {
System.out.println("BeforeInitialization : " + beanName);
return bean; // you can return any other object as well
}
public Object postProcessAfterInitialization(Object bean,
String beanName) throws BeansException {
System.out.println("AfterInitialization : " + beanName);
return bean; // you can return any other object as well
}
}
以下是MainApp.java 文件的內(nèi)容。在這里,需要注冊(cè)一個(gè)關(guān)閉掛鉤registerShutdownHook() 是在AbstractApplicationContext類中聲明的方法。這將確保正常關(guān)閉,并調(diào)用相關(guān)的destroy方法。
package com.yiibai;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
AbstractApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
context.registerShutdownHook();
}
}
下面是init和destroy方法需要的配置文件beans.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans 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-3.0.xsd">
<bean id="helloWorld" class="com.yiibai.HelloWorld"
init-method="init" destroy-method="destroy">
<property name="message" value="Hello World!"/>
</bean>
<bean class="com.yiibai.InitHelloWorld" />
</beans>
創(chuàng)建源代碼和bean配置文件完成后,讓我們運(yùn)行應(yīng)用程序。如果一切順利,這將打印以下信息:
BeforeInitialization : helloWorld Bean is going through init. AfterInitialization : helloWorld Your Message : Hello World! Bean will destroy now.
- Spring?BeanPostProcessor后處理器源碼解析
- 關(guān)于Spring BeanPostProcessor的執(zhí)行順序
- Spring BeanPostProcessor(后置處理器)的用法
- SpringBoot之通過BeanPostProcessor動(dòng)態(tài)注入ID生成器案例詳解
- Spring容器的創(chuàng)建過程之如何注冊(cè)BeanPostProcessor詳解
- 詳解使用Spring的BeanPostProcessor優(yōu)雅的實(shí)現(xiàn)工廠模式
- Spring中的后置處理器BeanPostProcessor詳解
- Spring BeanPostProcessor接口使用詳解
- spring中BeanPostProcessor的作用和使用注意事項(xiàng)
相關(guān)文章
SpringBoot?整合ChatGPT?API項(xiàng)目實(shí)戰(zhàn)教程
這篇文章主要介紹了SpringBoot整合ChatGPT API項(xiàng)目實(shí)戰(zhàn)教程,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
簡(jiǎn)單解析execute和submit有什么區(qū)別
這篇文章主要介紹了簡(jiǎn)單解析execute和submit有什么區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
java實(shí)現(xiàn)簡(jiǎn)單斗地主(看牌排序)
這篇文章主要介紹了java實(shí)現(xiàn)簡(jiǎn)單斗地主,看牌進(jìn)行排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2010-11-11
java樹結(jié)構(gòu)stream工具類的示例代碼詳解
Stream 作為 Java 8 的一大亮點(diǎn),它與 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念。今天通過本文重點(diǎn)給大家介紹java樹結(jié)構(gòu)stream工具類的示例代碼,感興趣的朋友一起看看吧2022-03-03
詳解 Java繼承關(guān)系下的構(gòu)造方法調(diào)用
這篇文章主要介紹了詳解 Java繼承關(guān)系下的構(gòu)造方法調(diào)用的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10
SpringBoot使用前綴樹過濾敏感詞的方法實(shí)例
Trie也叫做字典樹、前綴樹(Prefix Tree)、單詞查找樹,特點(diǎn):查找效率高,消耗內(nèi)存大,這篇文章主要給大家介紹了關(guān)于SpringBoot使用前綴樹過濾敏感詞的相關(guān)資料,需要的朋友可以參考下2022-01-01
java中實(shí)體類和JSON對(duì)象之間相互轉(zhuǎn)化
Java中關(guān)于Json格式轉(zhuǎn)化Object,Map,Collection類型和String類型之間的轉(zhuǎn)化在我們實(shí)際項(xiàng)目中應(yīng)用的很是普遍和廣泛。最近工作的過程中也是經(jīng)常有,因此,自己封裝了一個(gè)類分享給大家。2015-05-05

