溫馨提示×

溫馨提示×

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

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

spring如何實(shí)現(xiàn)兩個xml配置文件間的互調(diào)

發(fā)布時間:2020-09-22 17:20:03 來源:腳本之家 閱讀:163 作者:綠色的草 欄目:編程語言

這篇文章主要介紹了spring如何實(shí)現(xiàn)兩個xml配置文件間的互調(diào),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

首先建兩個測試類

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é)果是:

spring如何實(shí)現(xiàn)兩個xml配置文件間的互調(diào)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

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

AI