spring如何實現(xiàn)兩個xml配置文件間的互調(diào)
更新時間:2019年11月28日 09:57:05 作者:綠色的草
這篇文章主要介紹了spring如何實現(xiàn)兩個xml配置文件間的互調(diào),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
這篇文章主要介紹了spring如何實現(xiàn)兩個xml配置文件間的互調(diào),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
首先建兩個測試類
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);
}
}
然后針對兩類建兩個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="汪汪汪~"></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)在開始測試:
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é)果是:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何基于spring security實現(xiàn)在線用戶統(tǒng)計
這篇文章主要介紹了如何基于spring security實現(xiàn)在線用戶統(tǒng)計,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06
詳解SpringBoot中@SessionAttributes的使用
這篇文章主要通過示例為大家詳細介紹了SpringBoot中@SessionAttributes的使用,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2022-07-07
MyBatis標簽之Select?resultType和resultMap詳解
這篇文章主要介紹了MyBatis標簽之Select?resultType和resultMap,在MyBatis中有一個ResultMap標簽,它是為了映射select標簽查詢出來的結(jié)果集,下面使用一個簡單的例子,來介紹 resultMap 的使用方法,需要的朋友可以參考下2022-09-09

