ResultMain.java
package com.test6;
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/test6/applicationContext.xml");
UserService service=(UserService)context.getBean("test6.userServic"); // bean id를 명시해줌
System.out.println(service.getUserData());
}
}
================================================================================================================================================
UserImpl.java
package com.test6;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Component;
@Component("user") // <context:component-scan> 태그를 설정파일에 추가하면 해당 어노테이션이 적용된 클래스를 빈으로 등록하게 된다.
public class UserImpl implements User{
private String name;
private int age;
private String tel;
public String getName() {
return name;
}
@PostConstruct // 생성자를 부른 후에 값들을 초기화를 시킨다.
public void init(){
name="이이";
tel="010";
age=20;
System.out.println("초기화......@PostConstruct");
}
@PreDestroy
public void close(){
System.out.println("종료처리......@PostConstruct");
}
//@Required
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
@Override
public String result() {
String s=name+"님 나이는 "+age+"이고 전화는 "+tel;
return s;
}
}
================================================================================================================================================
================================================================================================================================================
<?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">
<!-- <context:annotation-config/> -->
<context:component-scan base-package="com.test6"/>
<!-- 요놈이 곧 bean 태그임, bean id를 만들때 class이름으로 만든다. 앞글자만 소문자로 바꿔서. 단, 어노테이션에 클래스이름을 따로 명시할 수 있다. -->
<!-- <bean id="user1" class="com.test6.UserImpl"> -->
<!-- <property name="name" value="연습용"/> -->
<!-- <property name="age" value="123"/> -->
<!-- </bean> -->
<!-- <bean id="user2" class="com.test6.UserImpl"> -->
<!-- <property name="name" value="ㅋㅋ"/> -->
<!-- <property name="age" value="10"/> -->
<!-- <property name="tel" value="0101010-0011"/> -->
<!-- </bean> -->
<!-- <bean id="userService" class="com.test6.UserService" /> 대신 <context:component-scan base-package="com.test6"/> 라고 명시함-->
</beans>
======================================================================================================================================================
user.properties
name=\uBC14\uBCF4\uB2D8
age=20
tel=010-2222-2222
======================================================================================================================================================
ResultMain.java를 실행시킨 결과
초기화......@PostConstruct
이이님 나이는 20이고 전화는 010
'spring > 예제' 카테고리의 다른 글
스프링 구조를 이해하는 예제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 |
스프링 구조를 이해하는 예제1(bean 태그의 이해) (0) | 2013.07.26 |