溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring中怎么實現(xiàn)兩個xml配置文件間的互調

發(fā)布時間:2021-08-10 17:05:16 來源:億速云 閱讀:114 作者:Leah 欄目:編程語言

這篇文章將為大家詳細講解有關spring中怎么實現(xiàn)兩個xml配置文件間的互調,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

首先建兩個測試類

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();  }}

關于spring中怎么實現(xiàn)兩個xml配置文件間的互調就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI