欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring如何使用xml裝配bean

 更新時(shí)間:2019年11月28日 08:34:36   作者:綠色的草  
這篇文章主要介紹了spring如何使用xml裝配bean,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了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é)果:

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

相關(guān)文章

最新評(píng)論