BeanPostProcessor接口定義了回調(diào)方法,可以實現(xiàn)這些方法來提供自己的實例化邏輯虏等、依賴項解析邏輯等弄唧。還可以在Spring容器完成實例化、配置和初始化bean后霍衫,通過插入一個或多個BeanPostProcessor實現(xiàn)來實現(xiàn)一些自定義邏輯候引。
可以配置多個BeanPostProcessor接口,并且可以通過設(shè)置order屬性來控制這些BeanPostProcessor接口的執(zhí)行順序敦跌,前提是BeanPostProcessor實現(xiàn)了Ordered接口澄干。
BeanPostProcessor操作bean(或?qū)ο螅嵗@意味著SpringIOC容器實例化一個bean實例峰髓,然后BeanPostProcessor接口完成它們的工作傻寂。
ApplicationContext自動檢測通過BeanPostProcessor接口的實現(xiàn)定義的任何bean,并將這些bean注冊為后處理器携兵,然后在創(chuàng)建bean時由容器適當(dāng)?shù)卣{(diào)用。
例子
下面的示例演示如何在ApplicationContext的上下文中編寫搂誉,注冊和使用BeanPostProcessors徐紧。
讓我們擁有一個可以正常運行的Eclipse IDE,并執(zhí)行以下步驟來創(chuàng)建一個Spring應(yīng)用程序:
HelloWorld.java文件的內(nèi)容-
package com.tutorialspoint;
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名稱∥旮梗可以在初始化bean之前和之后實現(xiàn)更復(fù)雜的邏輯嘲碧,因為可以在兩個后處理器方法中訪問bean對象。
InitHelloWorld.java文件的內(nèi)容-
package com.tutorialspoint;
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)容父阻。在這里愈涩,需要注冊一個在AbstractApplicationContext類上聲明的關(guān)閉鉤子registerShutdownHook()方法。這將確保正常關(guān)機并調(diào)用相關(guān)的destroy方法加矛。
package com.tutorialspoint;
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();
}
}
以下是初始化和銷毀方法所需的配置文件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.tutorialspoint.HelloWorld"
init-method = "init" destroy-method = "destroy">
<property name = "message" value = "Hello World!"/>
</bean>
<bean class = "com.tutorialspoint.InitHelloWorld" />
</beans>
完成創(chuàng)建源和Bean配置文件后履婉,運行該應(yīng)用程序。它將顯示以下消息-
BeforeInitialization : helloWorld
Bean is going through init.
AfterInitialization : helloWorld
Your Message : Hello World!
Bean will destroy now.
本文使用 文章同步助手 同步