spring如何實(shí)現(xiàn)兩個(gè)xml配置文件間的互調(diào)
這篇文章主要介紹了spring如何實(shí)現(xiàn)兩個(gè)xml配置文件間的互調(diào),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
首先建兩個(gè)測試類
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ì)兩類建兩個(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="汪汪汪~"></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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于spring事務(wù)傳播行為非事務(wù)方式的理解
這篇文章主要介紹了對(duì)spring事務(wù)傳播行為非事務(wù)方式的全面理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
詳解Nacos中注冊中心和配置中心的實(shí)現(xiàn)
Spring?Cloud?Alibaba?是阿里巴巴提供的一站式微服務(wù)開發(fā)解決方案。而?Nacos?作為?Spring?Cloud?Alibaba?的核心組件之一,提供了兩個(gè)非常重要的功能:注冊中心和配置中心,我們今天來了解和實(shí)現(xiàn)一下二者2022-08-08
Mybatis-Plus自動(dòng)填充的實(shí)現(xiàn)示例
這篇文章主要介紹了Mybatis-Plus自動(dòng)填充的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
MybatisPlus搭建項(xiàng)目環(huán)境及分頁插件
Mybatis-Plus(簡稱MP)是一個(gè)Mybatis的增強(qiáng)工具,在Mybatis的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生,下面這篇文章主要給大家介紹了關(guān)于MybatisPlus搭建項(xiàng)目環(huán)境及分頁插件的相關(guān)資料,需要的朋友可以參考下2022-11-11
如何基于spring security實(shí)現(xiàn)在線用戶統(tǒng)計(jì)
這篇文章主要介紹了如何基于spring security實(shí)現(xiàn)在線用戶統(tǒng)計(jì),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
詳解SpringBoot中@SessionAttributes的使用
這篇文章主要通過示例為大家詳細(xì)介紹了SpringBoot中@SessionAttributes的使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-07-07
Spring Boot 實(shí)現(xiàn)配置文件加解密原理
這篇文章主要介紹了Spring Boot 實(shí)現(xiàn)配置文件加解密原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Mybatis學(xué)習(xí)筆記之動(dòng)態(tài)SQL揭秘
這篇文章主要給大家介紹了關(guān)于Mybatis學(xué)習(xí)筆記之動(dòng)態(tài)SQL的相關(guān)資料,小編覺得挺不錯(cuò)的,對(duì)大家學(xué)習(xí)或者使用Mybatis會(huì)有一定的幫助,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
詳解IDEA中Debug的使用和進(jìn)制轉(zhuǎn)換問題
這篇文章主要介紹了IDEA中Debug的使用和進(jìn)制轉(zhuǎn)換,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
MyBatis標(biāo)簽之Select?resultType和resultMap詳解
這篇文章主要介紹了MyBatis標(biāo)簽之Select?resultType和resultMap,在MyBatis中有一個(gè)ResultMap標(biāo)簽,它是為了映射select標(biāo)簽查詢出來的結(jié)果集,下面使用一個(gè)簡單的例子,來介紹 resultMap 的使用方法,需要的朋友可以參考下2022-09-09

