<?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();
}
}
================================================================================================================================================
================================================================================================================================================
================================================================================================================================================
ResultMain.java를 실행시킨 결과
자바님 나이가 20세 이시군요!
'spring > 예제' 카테고리의 다른 글
스프링 구조를 이해하는 예제6(스프링의 annotation) (0) | 2013.07.27 |
---|---|
스프링 구조를 이해하는 예제5(외부에서 property 가져오기) (0) | 2013.07.27 |
스프링 구조를 이해하는 예제4(의존관계 자동 설정(autowire) 옵션) (0) | 2013.07.27 |
스프링 구조를 이해하는 예제3(map과 list의 property) (0) | 2013.07.27 |
스프링 구조를 이해하는 예제2(property의 사용) (0) | 2013.07.26 |