這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)spring中怎么利用xml裝配bean,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)主營蕪湖縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都APP應(yīng)用開發(fā),蕪湖縣h5成都微信小程序搭建,蕪湖縣網(wǎng)站營銷推廣歡迎蕪湖縣等地區(qū)企業(yè)咨詢
使用XML裝配bean,在bean中調(diào)用另一個bean方法,首先建一個Dog類和一個Cat類
package soundsystem;public class Dog {private String Cry;//叫聲//用setter方法注入public void setCry(String cry) { Cry = cry;}//定義一個狗叫方法 public void DogCry(){ System.out.println("狗叫:"+Cry); Cat.CatCry(); catEat.CatEating(); }}
package soundsystem;public class Cat { private String Cry;//叫聲//用構(gòu)造函數(shù)注入 public Cat(String cry){ this.Cry=cry; }//定義一個貓叫方法 public void CatCry(){ System.out.println("貓叫:"+Cry); }}
一個配置類Bean_DogXML.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="Dog" class="soundsystem.Dog"> <property name="Cry" value="汪汪汪~"></property> <property name="Cat" ref="Cat"></property> </bean> <bean id="Cat" class="soundsystem.Cat"> <constructor-arg value="喵~"></constructor-arg> </bean></beans>
現(xiàn)在開始測試
package Test;import org.junit.runner.RunWith;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import soundsystem.Cat;import soundsystem.Dog;@RunWith(SpringJUnit4ClassRunner.class)public class Test { @org.junit.Test public static void main(String[] args) { ApplicationContext ap=new ClassPathXmlApplicationContext("config/Bean_DogXML.xml"); Dog dog=(Dog)ap.getBean("Dog"); dog.DogCry(); }}
上述就是小編為大家分享的spring中怎么利用xml裝配bean了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:spring中怎么利用xml裝配bean
URL分享:http://www.muchs.cn/article12/gehsdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站設(shè)計、企業(yè)建站、網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計公司、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)