spring如何實(shí)現(xiàn)兩個(gè)xml配置文件間的互調(diào)
這篇文章主要介紹了spring如何實(shí)現(xiàn)兩個(gè)xml配置文件間的互調(diào),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
首先建兩個(gè)測(cè)試類(lèi)
package soundsystem; public class Dog { private String Cry; private Cat Cat; public void setCry(String cry) { Cry = cry; } public void setCat(soundsystem.Cat cat) { Cat = cat; } public void DogCry(){ System.out.println("狗叫:"+Cry); Cat.CatCry(); } }
package soundsystem; public class Cat { private String Cry; public Cat(String cry){ this.Cry=cry; } public void CatCry(){ System.out.println("貓叫:"+Cry); } }
然后針對(duì)兩類(lèi)建兩個(gè)xml配置文件
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"> <import resource="Bean_CatXML.xml"></import> <bean id="Dog" class="soundsystem.Dog"> <property name="Cry" value="汪汪汪~(yú)"></property> <property name="cat" ref="Cat"></property> </bean> </beans>
Bean_CatXML.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="Cat" class="soundsystem.Cat"> <constructor-arg value="喵喵~"></constructor-arg> </bean> </beans>
現(xiàn)在開(kāi)始測(cè)試:
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(); } }
輸出結(jié)果是:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡(jiǎn)述JAVA同步、異步、阻塞和非阻塞之間的區(qū)別
這篇文章主要介紹了JAVA同步、異步、阻塞和非阻塞之間的區(qū)別,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08MyBatis分頁(yè)查詢(xún)返回list的時(shí)候出現(xiàn)null的問(wèn)題
這篇文章主要介紹了MyBatis分頁(yè)查詢(xún)返回list的時(shí)候出現(xiàn)null的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07SpringBoot通過(guò)AOP與注解實(shí)現(xiàn)入?yún)⑿r?yàn)詳情
這篇文章主要介紹了SpringBoot通過(guò)AOP與注解實(shí)現(xiàn)入?yún)⑿r?yàn)詳情,文章從相關(guān)問(wèn)題展開(kāi)全文內(nèi)容詳情,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05struts2攔截器_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
如何使用struts2攔截器,或者自定義攔截器。下面通過(guò)實(shí)例代碼給大家分享struts2攔截器的相關(guān)知識(shí),感興趣的朋友參考下吧2017-09-09java中l(wèi)ong數(shù)據(jù)類(lèi)型轉(zhuǎn)換為int類(lèi)型
這篇文章主要講解Java中基本數(shù)據(jù)類(lèi)型,java long 類(lèi)型與其java int類(lèi)型的轉(zhuǎn)換的幾種方法,希望能給大家做一個(gè)參考2016-07-07mybatis plus代碼生成工具的實(shí)現(xiàn)代碼
這篇文章主要介紹了mybatis plus代碼生成工具的實(shí)現(xiàn)代碼,需要的朋友可以參考下2021-04-04