這期內容當中小編將會給大家?guī)碛嘘Pspring中怎么利用xml裝配bean,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

使用XML裝配bean,在bean中調用另一個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;//叫聲//用構造函數(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="/tupian/20230522/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="/tupian/20230522/ /tupian/20230522//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了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁標題:spring中怎么利用xml裝配bean-創(chuàng)新互聯(lián)
分享鏈接:http://www.chinadenli.net/article46/djdshg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計公司、網(wǎng)頁設計公司、電子商務、網(wǎng)站改版、App設計、微信公眾號
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容