這篇文章主要介紹“Spring抽象Bean和子Bean的定義與用法”,在日常操作中,相信很多人在Spring抽象Bean和子Bean的定義與用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Spring抽象Bean和子Bean的定義與用法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)公司是專業(yè)的米易網(wǎng)站建設(shè)公司,米易接單;提供成都網(wǎng)站制作、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行米易網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
一 配置
<?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實(shí)例 --> <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();}
三 實(shí)現(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í)行依賴關(guān)系注入..."); 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í)行依賴關(guān)系注入..."); 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í)行依賴關(guān)系注入...Spring執(zhí)行依賴關(guān)系注入...
到此,關(guān)于“Spring抽象Bean和子Bean的定義與用法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
本文名稱:Spring抽象Bean和子Bean的定義與用法
網(wǎng)站地址:http://muchs.cn/article34/geppse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、標(biāo)簽優(yōu)化、網(wǎng)站營銷、品牌網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)