빈의 방법으로 조립 XML 스프링

어셈블리를 사용하여 XML 콩, 콩의 다른 빈 메서드 호출의 개와 고양이 클래스를 구축하는 첫 번째 클래스

패키지 사운드 시스템을;

공공  클래스 개 { 
개인 문자열 외침; // 호출

// 주입 setter 메소드
공공 무효 setCry (문자열 크라이) { 크라이 = 울음; }

개 방법 정의 //
공공 무효 () {DogCry를 에서 System.out.println ( "개"+ 크라이); Cat.CatCry (); () catEat.CatEating; } }
패키지 사운드 시스템을;

공용  클래스 고양이 {
     개인 문자열 외침; // 호출
 
// 생성자 주입
공공의 고양이 (문자열 크라이) { 다음은이 .Cry = 외침; }
//라는 방법 고양이를 정의하는
공공 무효 () {CatCry를 에서 System.out.println ( "라는 고양이 :"+ 크라이); } }

 

구성 클래스 Bean_DogXML.xml

<? XML 버전 = "1.0" "UTF-8"인코딩 =?>
<콩의 xmlns = "http://www.springframework.org/schema/beans" 
       XMLNS : XSI = "http://www.w3.org/2001/XMLSchema-instance" 
       XSI :의 schemaLocation = "HTTP : // WWW .springframework.org / 스키마 / 콩 http://www.springframework.org/schema/beans/spring-beans.xsd ">

    <콩 ID = "개" 클래스 = "soundsystem.Dog">
        <속성 명 = "외침"값 = "汪汪 汪 ~"> </ property>를
        <속성 명 = "고양이"REF = "고양이"> </ property>를
    </ 콩>

    <콩 ID = "고양이" 클래스 = "soundsystem.Cat">
        <생성자 인수 값 = "喵 ~"> </ 생성자 ARG>
    </ 콩>

</ 콩>

 

이제 테스트를 시작

패키지 테스트;

수입 org.junit.runner.RunWith;
수입 org.springframework.context.ApplicationContext;
수입 org.springframework.context.support.ClassPathXmlApplicationContext;
수입 org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
수입 soundsystem.Cat;
수입 soundsystem.Dog;

@RunWith (SpringJUnit4ClassRunner. 클래스 )
 공용  클래스 테스트 {

    @ org.junit.Test
    공공  정적  무효 메인 (문자열 []에 args) {
        ApplicationContext의 AP는 = 새로운 ClassPathXmlApplicationContext ( "설정 / Bean_DogXML.xml을" );
        개 개 = (개) ap.getBean ( "개" );
        dog.DogCry ();
    }
}

 

출력 :

 

추천

출처www.cnblogs.com/yyy116008/p/11938639.html