ResultMain.java
package com.test4;
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/test4/applicationContext.xml");
UserService service=(UserService)context.getBean("userService"); // bean id를 명시해줌
System.out.println(service.getUserData());
}
}
================================================================================================================================================
User.java
package com.test2;
public interface User {
public String result();
}
================================================================================================================================================
================================================================================================================================================
================================================================================================================================================
<?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="user" class="com.test4.UserImpl">
<property name="name" value="스프링"/>
<property name="age" value="10"/>
<property name="tel" value="010-111-1111"/>
</bean>
<!--
의존관계 자동 설정(autowire)
byName : 프로퍼티와 동일한 이름을 갖는 빈객체설정(setter 필요)
byType : 프로퍼티타입과 같은 빈객체를 설정
constructor : 생성자 파라미터 타입과 같은 타입의 빈객체를 생성자에 전달
autodetect : constructor 방식을 적용하고 없으면 byType 방식으로 의존관계 설정
-->
<bean id="userService" class="com.test4.UserService" autowire="byName"/>
</beans>
======================================================================================================================================================
ResultMain.java를 실행시킨 결과
스프링님 나이는 10이고 전화는 010-111-1111
'spring > 예제' 카테고리의 다른 글
스프링 구조를 이해하는 예제6(스프링의 annotation) (0) | 2013.07.27 |
---|---|
스프링 구조를 이해하는 예제5(외부에서 property 가져오기) (0) | 2013.07.27 |
스프링 구조를 이해하는 예제3(map과 list의 property) (0) | 2013.07.27 |
스프링 구조를 이해하는 예제2(property의 사용) (0) | 2013.07.26 |
스프링 구조를 이해하는 예제1(bean 태그의 이해) (0) | 2013.07.26 |