Spring實(shí)戰(zhàn)之容器后處理器操作示例
本文實(shí)例講述了Spring實(shí)戰(zhàn)之容器后處理器。分享給大家供大家參考,具體如下:
一 配置文件
<?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"> <!-- 配置兩個(gè)簡(jiǎn)單Bean實(shí)例 --> <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/> <bean id="chinese" class="org.crazyit.app.service.impl.Chinese" init-method="init" p:name="孫悟空" p:axe-ref="steelAxe"/> <!-- 配置容器后處理器 --> <bean id="beanFactoryPostProcessor" class="org.crazyit.app.util.MyBeanFactoryPostProcessor"/> </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實(shí)例化主調(diào)bean:Chinese實(shí)例..."); } public void setAxe(Axe axe) { this.axe = axe; } public void setName(String name) { System.out.println("Spring執(zhí)行setName()方法注入依賴(lài)關(guān)系..."); this.name = name; } public void useAxe() { System.out.println(name + axe.chop()); } // 下面是兩個(gè)生命周期方法 public void init() { System.out.println("正在執(zhí)行初始化方法 init..."); } public void afterPropertiesSet() throws Exception { System.out.println("正在執(zhí)行初始化方法 afterPropertiesSet..."); } }
SteelAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class SteelAxe implements Axe { public SteelAxe() { System.out.println("Spring實(shí)例化依賴(lài)bean:SteelAxe實(shí)例..."); } public String chop() { return "鋼斧砍柴真快"; } }
四 容器后處理器
package org.crazyit.app.util; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { /** * 重寫(xiě)該方法,對(duì)Spring進(jìn)行后處理。 * @param beanFactory Spring容器本身 */ public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory) throws BeansException { System.out.println("程序?qū)pring所做的BeanFactory的初始化沒(méi)有改變..."); System.out.println("Spring容器是:" + beanFactory); } }
五 測(cè)試類(lèi)
package lee; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.crazyit.app.service.*; public class BeanTest { public static void main(String[] args) { // 以ApplicationContex作為Spring容器 // 它會(huì)自動(dòng)注冊(cè)容器后處理器、Bean后處理器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Person p = (Person)ctx.getBean("chinese"); p.useAxe(); } }
六 測(cè)試結(jié)果
程序?qū)pring所做的BeanFactory的初始化沒(méi)有改變...
Spring容器是:org.springframework.beans.factory.support.DefaultListableBeanFactory@6a024a67: defining beans [steelAxe,chinese,beanFactoryPostProcessor]; root of factory hierarchy
Spring實(shí)例化依賴(lài)bean:SteelAxe實(shí)例...
Spring實(shí)例化主調(diào)bean:Chinese實(shí)例...
Spring執(zhí)行setName()方法注入依賴(lài)關(guān)系...
正在執(zhí)行初始化方法 afterPropertiesSet...
正在執(zhí)行初始化方法 init...
孫悟空鋼斧砍柴真快
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Spring框架入門(mén)與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
基于springboot搭建的web系統(tǒng)架構(gòu)的方法步驟
這篇文章主要介紹了基于springboot搭建的web系統(tǒng)架構(gòu)的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04Spring Boot結(jié)合ECharts案例演示示例
本文主要主要介紹了Spring Boot結(jié)合ECharts案例演示示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Spring data jpa的使用與詳解(復(fù)雜動(dòng)態(tài)查詢(xún)及分頁(yè),排序)
這篇文章主要介紹了Spring data jpa的使用與詳解(復(fù)雜動(dòng)態(tài)查詢(xún)及分頁(yè),排序),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11關(guān)于spring boot中幾種注入方法的一些個(gè)人看法
這篇文章主要給大家介紹了關(guān)于spring boot中幾種注入方法的一些個(gè)人看法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07java final 和instanceof 關(guān)鍵字的區(qū)別
這篇文章介紹了java final 和instanceof 關(guān)鍵字的區(qū)別,有需要的朋友可以參考一下2013-09-09