Spring實戰(zhàn)之Bean的后處理器操作示例
本文實例講述了Spring實戰(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個普通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執(zhí)行setName()方法注入依賴關系...");
this.name = name;
}
public void useAxe()
{
System.out.println(name + axe.chop());
}
// 下面是兩個生命周期方法
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實例化依賴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執(zhí)行setName()方法注入依賴關系...
Bean后處理器在初始化之前對chinese進行增強處理...
正在執(zhí)行初始化方法 afterPropertiesSet...
正在執(zhí)行初始化方法 init...
Bean后處理器在初始化之后對chinese進行增強處理...
Spring執(zhí)行setName()方法注入依賴關系...
瘋狂iOS講義鋼斧砍柴真快
更多關于java相關內容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
相關文章
Java使用Arrays.sort()方法實現(xiàn)給對象排序
這篇文章主要介紹了Java使用Arrays.sort()方法實現(xiàn)給對象排序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Java設計模式之解釋器模式(Interpreter模式)介紹
這篇文章主要介紹了Java設計模式之解釋器模式(Interpreter模式)介紹,Interpreter定義:定義語言的文法,并且建立一個解釋器來解釋該語言中的句子,需要的朋友可以參考下2015-03-03
Mybatis 如何批量刪除數(shù)據(jù)的實現(xiàn)示例
這篇文章主要介紹了Mybatis 如何批量刪除數(shù)據(jù)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03

