Spring實戰之Bean的後處理器操作示例
阿新 • • 發佈:2020-01-07
本文例項講述了Spring實戰之Bean的後處理器操作。分享給大家供大家參考,具體如下:
一 配置檔案
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 配置2個普通Bean例項 --> <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/> <bean id="chinese" class="org.crazyit.app.service.impl.Chinese" init-method="init" p:axe-ref="steelAxe" p:name="依賴注入的值"/> <!-- 配置Bean後處理器,可以無需指定id屬性 --> <bean id="bp" class="org.crazyit.app.util.MyBeanPostProcessor"/> </beans>
二 介面
Axe
package org.crazyit.app.service; public interface Axe { public String chop(); }
Person
package org.crazyit.app.service; public interface Person { public void useAxe(); }
三 Bean
Chinese
package org.crazyit.app.service.impl; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.crazyit.app.service.*; public class Chinese implements Person,InitializingBean { private Axe axe; private String name; public Chinese() { System.out.println("Spring例項化主調bean:Chinese例項..."); } public void setAxe(Axe axe) { this.axe = axe; } public void setName(String name) { System.out.println("Spring執行setName()方法注入依賴關係..."); this.name = name; } public void useAxe() { System.out.println(name + axe.chop()); } // 下面是兩個生命週期方法 public void init() { System.out.println("正在執行初始化方法 init..."); } public void afterPropertiesSet() throws Exception { System.out.println("正在執行初始化方法 afterPropertiesSet..."); } }
SteelAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class SteelAxe implements Axe { public SteelAxe() { System.out.println("Spring例項化依賴bean:SteelAxe例項..."); } public String chop() { return "鋼斧砍柴真快"; } }
四 Bean後處理器
package org.crazyit.app.util; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.crazyit.app.service.*; import org.crazyit.app.service.impl.*; public class MyBeanPostProcessor implements BeanPostProcessor { /** * 對容器中的Bean例項進行後處理 * @param bean 需要進行後處理的原Bean例項 * @param beanName 需要進行後處理的Bean的配置id * @return 返回後處理完成後的Bean */ public Object postProcessBeforeInitialization (Object bean,String beanName) { System.out.println("Bean後處理器在初始化之前對" + beanName + "進行增強處理..."); // 返回的處理後的Bean例項,該例項就是容器中實際使用的Bean // 該Bean例項甚至可與原Bean截然不同 return bean; } public Object postProcessAfterInitialization (Object bean,String beanName) { System.out.println("Bean後處理器在初始化之後對" + beanName + "進行增強處理..."); // 如果該Bean是Chinese類的例項 if (bean instanceof Chinese) { // 修改其name成員變數 Chinese c = (Chinese)bean; c.setName("瘋狂iOS講義"); } return bean; } }
五 測試結果
Spring例項化主調bean:Chinese例項...
Spring例項化依賴bean:SteelAxe例項...
Bean後處理器在初始化之前對steelAxe進行增強處理...
Bean後處理器在初始化之後對steelAxe進行增強處理...
Spring執行setName()方法注入依賴關係...
Bean後處理器在初始化之前對chinese進行增強處理...
正在執行初始化方法 afterPropertiesSet...
正在執行初始化方法 init...
Bean後處理器在初始化之後對chinese進行增強處理...
Spring執行setName()方法注入依賴關係...
瘋狂iOS講義鋼斧砍柴真快
更多關於java相關內容感興趣的讀者可檢視本站專題:《Spring框架入門與進階教程》、《Java資料結構與演算法教程》、《Java操作DOM節點技巧總結》、《Java檔案與目錄操作技巧彙總》和《Java快取操作技巧彙總》
希望本文所述對大家java程式設計有所幫助。