Spring實戰(zhàn)之抽象Bean和子Bean定義與用法示例
本文實例講述了Spring實戰(zhàn)之抽象Bean和子Bean定義與用法。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 定義Axe實例 --> <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/> <!-- 指定abstract="true"定義抽象Bean --> <bean id="personTemplate" abstract="true"> <property name="name" value="crazyit"/> <property name="axe" ref="steelAxe"/> </bean> <!-- 通過指定parent屬性指定下面Bean配置可從父Bean繼承得到配置信息 --> <bean id="chinese" class="org.crazyit.app.service.impl.Chinese" parent="personTemplate"/> <bean id="american" class="org.crazyit.app.service.impl.American" parent="personTemplate"/> </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(); }
三 實現(xiàn)類
1 American
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class American implements Person { private Axe axe; private String name; public void setAxe(Axe axe) { System.out.println("Spring執(zhí)行依賴關系注入..."); this.axe = axe; } public void setName(String name) { this.name = name; } public void useAxe() { System.out.println("我是美國人:名字為:" + name + "。正在用斧頭" + axe.chop()); } }
2 Chinese
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class Chinese implements Person { private Axe axe; private String name; public void setAxe(Axe axe) { System.out.println("Spring執(zhí)行依賴關系注入..."); this.axe = axe; } public void setName(String name) { this.name = name; } public void useAxe() { System.out.println("我是中國人:名字為:" + name + "。正在用斧頭" + axe.chop()); } }
3 SteelAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class SteelAxe implements Axe { public String chop() { return "鋼斧砍柴真快"; } }
4 StoneAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class StoneAxe implements Axe { public String chop() { return "石斧砍柴好慢"; } }
四 測試類
package lee; import org.springframework.context.ApplicationContext; import org.springframework.context.support.*; import org.crazyit.app.service.*; public class BeanTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); } }
五 測試結(jié)果
Spring執(zhí)行依賴關系注入...
Spring執(zhí)行依賴關系注入...
更多關于java相關內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
相關文章
基于java+springboot+mybatis+laiyu實現(xiàn)學科競賽管理系統(tǒng)
這篇文章主要介紹了基于java+springboot+mybatis+laiyu實現(xiàn)的學科競賽管理系統(tǒng),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09SpringMVC4+MyBatis+SQL Server2014實現(xiàn)數(shù)據(jù)庫讀寫分離
這篇文章主要介紹了SpringMVC4+MyBatis+SQL Server2014實現(xiàn)讀寫分離,需要的朋友可以參考下2017-04-04SpringBoot Actuator埋點和監(jiān)控及簡單使用
最近做的項目涉及到埋點監(jiān)控、報表、日志分析的相關知識,于是搗鼓的一番,下面把涉及的知識點及SpringBoot Actuator埋點和監(jiān)控的簡單用法,給大家分享下,感興趣的朋友一起看看吧2021-11-11springboot~ObjectMapper~dto到entity的自動賦值
這篇文章主要介紹了springboot~ObjectMapper~dto到entity的自動賦值,本文分三種情況給大家介紹,需要的朋友可以參考下2018-08-08