解析Java的Spring框架的BeanPostProcessor發(fā)布處理器
BeanPostProcessor 的接口定義,可以實現(xiàn)提供自己的實例化邏輯,依賴解析邏輯等,也可以以后在Spring容器實例化完畢,配置和初始化一個bean通過插入一個或多個的BeanPostProcessor實現(xiàn)一些自定義邏輯回調(diào)方法實現(xiàn)。
可以配置多個的BeanPostProcessor接口,控制這些的BeanPostProcessor接口,通過設(shè)置屬性順序執(zhí)行順序提供的BeanPostProcessor實現(xiàn)了Ordered接口。
BeanPostProcessor可以對bean(或?qū)ο螅┎僮鲗嵗?,這意味著Spring IoC容器實例化一個bean實例,然后BeanPostProcessor的接口做好自己的工作。
ApplicationContext會自動檢測已定義實現(xiàn)的BeanPostProcessor接口和注冊這些bean類為后置處理器,可然后通過在容器創(chuàng)建bean,在適當時候調(diào)用任何bean。
示例:
下面的示例顯示如何編寫,注冊和使用BeanPostProcessor 可以在一個ApplicationContext 的上下文。
使用Eclipse IDE,然后按照下面的步驟來創(chuàng)建一個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."); } }
這是實現(xiàn)BeanPostProcessor,之前和之后的任何bean的初始化它打印一個bean的名字非常簡單的例子。可以因為有兩個后處理器的方法對內(nèi)部bean對象訪問之前和實例化一個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)容。在這里,需要注冊一個關(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ī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動態(tài)注入ID生成器案例詳解
- Spring容器的創(chuàng)建過程之如何注冊BeanPostProcessor詳解
- 詳解使用Spring的BeanPostProcessor優(yōu)雅的實現(xiàn)工廠模式
- Spring中的后置處理器BeanPostProcessor詳解
- Spring BeanPostProcessor接口使用詳解
- spring中BeanPostProcessor的作用和使用注意事項
相關(guān)文章
SpringBoot?整合ChatGPT?API項目實戰(zhàn)教程
這篇文章主要介紹了SpringBoot整合ChatGPT API項目實戰(zhàn)教程,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05java樹結(jié)構(gòu)stream工具類的示例代碼詳解
Stream 作為 Java 8 的一大亮點,它與 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念。今天通過本文重點給大家介紹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