<?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">
<!-- 디폴트생성자로 객체생성 및 프로퍼티설정( 해당 class에 setter 필요) -->
<!--
<bean id="user1" class="com.test2.UserImpl">
<property name="name" value="안보여"/> property : xml에서 변수를 만들어 넘겨줄 때 사용
<property name="age" value="20"/>
<property name="tel"> <property name="tel" value="010-1111-1111"/> 와 똑같다
<value>010-1111-1111</value>
</property>
</bean>
-->
<bean id="user1" class="com.test2.UserImpl" p:name="밥먹자" p:age="10" p:tel="010"/><!-- p:사용할변수이름=사용할변수의밸류 -->
<!-- 프로퍼티를 의존관계 설정( 해당 class에 setter 필요) -->
<!--
<bean id="userService" class="com.test2.UserService">
<property name="user" ref="user1"/>
</bean>
-->
<!-- scope 속성 : 빈의 범위 설정
singleton : 스프링 컨테이너에 한 개의 빈 객체만 설정(기본)
prototype : 빈을 사용할 때마다 객체를 생성
-->
<!-- -XML 네임스페이스로 프로퍼티 의존관계 설정(setter 필요) xmlns:p="http://www.springframework.org/schema/p" 가 필요 -->
<bean id="userService" class="com.test2.UserService" p:user-ref="user1" scope="prototype"/>
</beans>
======================================================================================================================================================
ResultMain.java
package com.test2;
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/test2/applicationContext.xml");
UserService service=(UserService)context.getBean("userService"); // bean id를 명시해줌
UserService service1=(UserService)context.getBean("userService");
//객체를 많이 만들어도 동일한 객체임(싱글톤 패턴 - 모든 인스턴스의 참조주소가 똑같다) - 설정파일에 scope를 선언하지 않으면 설정됨. 이것이 기본설정임
// 설정파일에 scope를 prototype이라고 명시할 경우 만들때 마다 다른 객체 생성
System.out.println(service.getUserData());
System.out.println(service==service1);
System.out.println(service);
System.out.println(service1);
}
}
================================================================================================================================================
User.java
package com.test2;
public interface User {
public String result();
}
================================================================================================================================================
================================================================================================================================================
================================================================================================================================================
ResultMain.java를 실행시킨 결과
밥먹자님 나이는 10이고 전화는 010
false
com.test2.UserService@63b6f0eb
com.test2.UserService@7f830771
'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 |
스프링 구조를 이해하는 예제1(bean 태그의 이해) (0) | 2013.07.26 |