본문 바로가기

spring/예제

스프링 구조를 이해하는 예제1(bean 태그의 이해)


applicationContext.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"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="

    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd

        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">          

<!-- 인자가 없는 생성자를 통한 객체 생성 -->

<!-- <bean id="testService1" class="com.test1.TestServicImpl"/> -->


<!-- 인자가 있는 생성자로 객체 생성 -->

<bean id="testService1" class="com.test1.TestServicImpl">

<constructor-arg value="자바"/><!-- 프리미티형은 value, reference는 ref를 이용하여 객체 생성 -->

<constructor-arg value="20"/>

</bean>


<!-- 인자가 있는 생성자를 통한 객체 생성 -->

<!--  생성자를 이용한 의존 관계 설정 -->

<bean id="test" class="com.test1.Test">

<!-- <constructor-arg><ref bean="testService1"/></constructor-arg> -->

<constructor-arg ref="testService1"></constructor-arg><!-- 위 아래 똑같다. -->

</bean>

</beans> 


======================================================================================================================================================

ResultMain.java


package com.test1;


import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class ResultMain {

public static void main(String[] args){

// 스프링 컨테이너(생성된 자바 객체를 저장한곳) 생성

AbstractApplicationContext context=new ClassPathXmlApplicationContext("com/test1/applicationContext.xml");

// 스프링 컨테이너에서 객체를 가져옴

// Test test=(Test)context.getBean("test");

Test test=context.getBean(Test.class);

System.out.println(test.getTestDate());

}

}


================================================================================================================================================

Test.java


package com.test1;


public class Test {

private TestService testService;

public Test(TestService testService){

this.testService=testService;

}

public String getTestDate(){

return testService.result();

}

}


================================================================================================================================================

TestService.java

package com.test1;

public interface TestService {
public String result();
}


================================================================================================================================================

TestServicImpl.java

package com.test1;

public class TestServicImpl implements TestService {
private String name;
private int age;
public TestServicImpl(){
this.name="스프링";
this.age=10;
}
public TestServicImpl(String name, int age){
this.name=name;
this.age=age;
}

@Override
public String result() {
String message=name + "님 나이가 " + age + "세 이시군요!";
return message;
}
}

================================================================================================================================================


ResultMain.java를 실행시킨 결과


자바님 나이가 20세 이시군요!