這篇文章主要介紹Spring框架設(shè)值注入操作的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括宜春網(wǎng)站建設(shè)、宜春網(wǎng)站制作、宜春網(wǎng)頁制作以及宜春網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,宜春網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到宜春省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
一 配置
<?xml version="1.0" encoding="GBK"?> <!-- Spring配置文件的根元素,使用spring-beans-4.0.xsd語義約束 --> <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"> <!-- 配置chinese實(shí)例,其實(shí)現(xiàn)類是Chinese類 --> <bean id="chinese" class="org.crazyit.app.service.impl.Chinese"> <!-- 驅(qū)動(dòng)調(diào)用chinese的setAxe()方法,將容器中stoneAxe作為傳入?yún)?shù) --> <property name="axe" ref="stoneAxe"/> </bean> <!-- 配置stoneAxe實(shí)例,其實(shí)現(xiàn)類是StoneAxe --> <bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/> <!-- 配置steelAxe實(shí)例,其實(shí)現(xiàn)類是SteelAxe --> <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/> </beans>
二 接口
Axe
package org.crazyit.app.service; public interface Axe { // Axe接口里有個(gè)砍的方法 public String chop(); }
Person
package org.crazyit.app.service; public interface Person { // 定義一個(gè)使用斧子的方法 public void useAxe(); }
三 實(shí)現(xiàn)
Chinese
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class Chinese implements Person { private Axe axe; // 設(shè)值注入所需的setter方法 public void setAxe(Axe axe) { this.axe = axe; } // 實(shí)現(xiàn)Person接口的useAxe方法 public void useAxe() { // 調(diào)用axe的chop()方法, // 表明Person對(duì)象依賴于axe對(duì)象 System.out.println(axe.chop()); } }
StoneAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class StoneAxe implements Axe { public String chop() { return "石斧砍柴好慢"; } }
SteelAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class SteelAxe implements Axe { public String chop() { return "鋼斧砍柴真快"; } }
四 測試類
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)throws Exception { // 創(chuàng)建Spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 獲取chinese 實(shí)例 Person p = ctx.getBean("chinese" , Person.class); // 調(diào)用useAxe()方法 p.useAxe(); } }
以上是“Spring框架設(shè)值注入操作的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享標(biāo)題:Spring框架設(shè)值注入操作的示例分析
網(wǎng)站網(wǎng)址:http://bm7419.com/article24/jddgje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、域名注冊(cè)、ChatGPT、建站公司、微信公眾號(hào)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)