这篇文章主要介绍了春天如何实现两个xml配置文件间的互调,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
首先建两个测试类
包soundsystem; 公共类狗{ 私人字符串哭; 私人猫猫; 公共空间setCry(字符串哭){ 哭=哭; } 公共空间setCat (soundsystem。猫猫){ 猫=猫; } 公共空间DogCry () { System.out.println(“狗叫:“+哭); Cat.CatCry (); } }
包soundsystem; 公共类猫{ 私人字符串哭; 公共的猫(字符串哭){ this.Cry=哭; } 公共空间CatCry () { System.out.println(“猫叫:“+哭); } }
然后针对两类建两个xml配置文件
Bean_DogXML.xml
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;豆类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”比; & lt;进口资源=" Bean_CatXML.xml "祝辞& lt;/import> & lt; bean id=肮贰崩?皊oundsystem.Dog”比; & lt;属性名="哭" value=" https://www.yisu.com/zixun/汪汪~汪”祝辞& lt;/property> & lt;属性名="猫" ref=懊ā痹? lt;/property> & lt;/bean> & lt;/beans>
Bean_CatXML.xml
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;豆类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”比; & lt; bean id=懊ā崩?皊oundsystem.Cat”比; & lt; constructor-arg值=" https://www.yisu.com/zixun/喵喵~”祝辞& lt;/constructor-arg> & lt;/bean> & lt;/beans>
现在开始测试:
包装测试; 进口org.junit.runner.RunWith; 进口org.springframework.context.ApplicationContext; 进口org.springframework.context.support.ClassPathXmlApplicationContext; 进口org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 进口soundsystem.Cat; 进口soundsystem.Dog; @RunWith (SpringJUnit4ClassRunner.class) 公共类测试{ @org.junit.Test 公共静态void main (String [] args) { 据美联社=new ClassPathXmlApplicationContext ApplicationContext(“配置/Bean_DogXML.xml”); 狗狗=(狗)ap.getBean(“狗”); dog.DogCry (); } }
输出结果是:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。