Spring實(shí)戰(zhàn)之Bean的后處理器操作示例
本文實(shí)例講述了Spring實(shí)戰(zhàn)之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個(gè)普通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: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實(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()方法注入依賴關(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í)例化依賴bean:SteelAxe實(shí)例...");
}
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
{
/**
* 對(duì)容器中的Bean實(shí)例進(jìn)行后處理
* @param bean 需要進(jìn)行后處理的原Bean實(shí)例
* @param beanName 需要進(jìn)行后處理的Bean的配置id
* @return 返回后處理完成后的Bean
*/
public Object postProcessBeforeInitialization
(Object bean , String beanName)
{
System.out.println("Bean后處理器在初始化之前對(duì)"
+ beanName + "進(jìn)行增強(qiáng)處理...");
// 返回的處理后的Bean實(shí)例,該實(shí)例就是容器中實(shí)際使用的Bean
// 該Bean實(shí)例甚至可與原Bean截然不同
return bean;
}
public Object postProcessAfterInitialization
(Object bean , String beanName)
{
System.out.println("Bean后處理器在初始化之后對(duì)"
+ beanName + "進(jìn)行增強(qiáng)處理...");
// 如果該Bean是Chinese類的實(shí)例
if (bean instanceof Chinese)
{
// 修改其name成員變量
Chinese c = (Chinese)bean;
c.setName("瘋狂iOS講義");
}
return bean;
}
}
五 測(cè)試結(jié)果
Spring實(shí)例化主調(diào)bean:Chinese實(shí)例...
Spring實(shí)例化依賴bean:SteelAxe實(shí)例...
Bean后處理器在初始化之前對(duì)steelAxe進(jìn)行增強(qiáng)處理...
Bean后處理器在初始化之后對(duì)steelAxe進(jìn)行增強(qiáng)處理...
Spring執(zhí)行setName()方法注入依賴關(guān)系...
Bean后處理器在初始化之前對(duì)chinese進(jìn)行增強(qiáng)處理...
正在執(zhí)行初始化方法 afterPropertiesSet...
正在執(zhí)行初始化方法 init...
Bean后處理器在初始化之后對(duì)chinese進(jìn)行增強(qiáng)處理...
Spring執(zhí)行setName()方法注入依賴關(guān)系...
瘋狂iOS講義鋼斧砍柴真快
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
mybatis使用foreach查詢不出結(jié)果也不報(bào)錯(cuò)的問題
這篇文章主要介紹了mybatis使用foreach查詢不出結(jié)果也不報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java實(shí)現(xiàn)字符串倒序輸出的四種方法匯總
這篇文章主要介紹了Java實(shí)現(xiàn)字符串倒序輸出的四種方法匯總,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Java使用Arrays.sort()方法實(shí)現(xiàn)給對(duì)象排序
這篇文章主要介紹了Java使用Arrays.sort()方法實(shí)現(xiàn)給對(duì)象排序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java設(shè)計(jì)模式之解釋器模式(Interpreter模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之解釋器模式(Interpreter模式)介紹,Interpreter定義:定義語言的文法,并且建立一個(gè)解釋器來解釋該語言中的句子,需要的朋友可以參考下2015-03-03
Mybatis 如何批量刪除數(shù)據(jù)的實(shí)現(xiàn)示例
這篇文章主要介紹了Mybatis 如何批量刪除數(shù)據(jù)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

