spring如何使用xml裝配bean
這篇文章主要介紹了spring如何使用xml裝配bean,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
使用XML裝配bean,在bean中調(diào)用另一個(gè)bean方法,首先建一個(gè)Dog類和一個(gè)Cat類
package soundsystem;
public class Dog {
private String Cry;//叫聲
//用setter方法注入
public void setCry(String cry) {
Cry = cry;
}
//定義一個(gè)狗叫方法
public void DogCry(){
System.out.println("狗叫:"+Cry);
Cat.CatCry();
catEat.CatEating();
}
}
package soundsystem;
public class Cat {
private String Cry;//叫聲
//用構(gòu)造函數(shù)注入
public Cat(String cry){
this.Cry=cry;
}
//定義一個(gè)貓叫方法
public void CatCry(){
System.out.println("貓叫:"+Cry);
}
}
一個(gè)配置類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">
<bean id="Dog" class="soundsystem.Dog">
<property name="Cry" value="汪汪汪~"></property>
<property name="Cat" ref="Cat"></property>
</bean>
<bean id="Cat" class="soundsystem.Cat">
<constructor-arg value="喵~"></constructor-arg>
</bean>
</beans>
現(xiàn)在開始測(cè)試
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é)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于XML配置Spring的自動(dòng)裝配過程解析
- Spring裝配Bean教程之XML安裝配置bean詳解
- sersync2 完全安裝配置說明(二) 可選功能與xml高級(jí)配置
- Spring注解實(shí)現(xiàn)Bean自動(dòng)裝配示例詳解
- Spring IOC裝配Bean過程解析
- Spring自動(dòng)裝配Bean實(shí)現(xiàn)過程詳解
- spring boot中的條件裝配bean的實(shí)現(xiàn)
- spring裝配bean的3種方式總結(jié)
- 在Spring中自動(dòng)裝配Bean的屬性
- spring在IoC容器中裝配Bean詳解
- 詳解SpringBean基于XML的裝配
相關(guān)文章
淺析Java數(shù)據(jù)庫(kù)操作工具包jOOQ的使用
jOOQ?是一個(gè)輕量級(jí)的?Java?ORM(對(duì)象關(guān)系映射)框架,可用來構(gòu)建復(fù)雜的?SQL?查詢,這篇文章主要來和大家介紹一下jOOQ的使用,需要的可以參考下2024-04-04
Java實(shí)現(xiàn)線程按序交替執(zhí)行的方法詳解
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)線程按序交替執(zhí)行,文中的示例代碼講解詳細(xì),對(duì)我們了解線程有一定幫助,需要的可以參考一下2022-10-10
Java實(shí)現(xiàn)導(dǎo)出Excel功能
通過java中Controller層,來接受請(qǐng)求,數(shù)據(jù)庫(kù)查詢到的數(shù)據(jù)進(jìn)行封裝,然后使用ExcelUtils進(jìn)行輸出,接下來通過本文給大家分享Java實(shí)現(xiàn)導(dǎo)出Excel功能的實(shí)例代碼,感興趣的朋友跟隨小編一起看看吧2021-11-11
springMVC之HandlerExceptionResolver使用
這篇文章主要介紹了springMVC之HandlerExceptionResolver使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
SchedulingConfigurer實(shí)現(xiàn)動(dòng)態(tài)定時(shí),導(dǎo)致ApplicationRunner無(wú)效解決
這篇文章主要介紹了SchedulingConfigurer實(shí)現(xiàn)動(dòng)態(tài)定時(shí),導(dǎo)致ApplicationRunner無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05

