본문 바로가기

콩's EDUCATION/콩's SPRING

ApplicationContext 객체 xml 활용



package user;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class UserTest {

public static void main(String[] args){

System.out.println("1. XML 입력 전");

ApplicationContext user = new 

ClassPathXmlApplicationContext("user/userservice.xml");

System.out.println("2. XML 입력 후");

UserServiceImpl userservice = (UserServiceImpl)user.getBean("userservice");

// 혹은 UserService user = (UserService)user.getBean("userservice");

UserVo vo = (UserVo)user.getBean("vo");

userservice.addUser(vo);

}

}



<콘솔 결과>


1. XML 입력 전

2014. 7. 25 오전 11:04:49 org.springframework.context.support.AbstractApplicationContext prepareRefresh

정보: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14d3343: startup date [Fri Jul 25 11:04:49 KST 2014]; root of context hierarchy

2014. 7. 25 오전 11:04:49 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

정보: Loading XML bean definitions from class path resource [user/userservice.xml]

2014. 7. 25 오전 11:04:50 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

정보: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b9ce4b: defining beans [userservice,vo]; root of factory hierarchy

UserServiceImpl 기본 생성자

UserVo 생성자 호출

2. XML 입력 후

addUser 호출


user.zip


xml에서 lazy-init 속성을 사용하면 필요하지 않은 객체 생성 시기를 조절할 수 있다.

depends-on 속성을 사용하면 해당 객체가 생성시에 우선 순위를 줄 수 있다.


xml에 아래 5가지 속성을 부여하면 다음과 같다.


lazy-init="default"          : 필요하지 않은 객체 생성 시기를 조절

depends-on="vo"              : 해당 객체를 생성할 때 우선 순위를 줄 수 있음

factory-method="getInstance" : 객체 생성

init-method="init"           : 객체 초기화

destroy-method="destroy"     : 객체 소멸


userservice.xml

<bean factory-method="getInstance"

init-method="init"

destroy-method="destroy" id="userservice" class="user.UserServiceImpl"></bean>


UserServiceImpl.java

public static UserServiceImpl getInstance(){

System.out.println("UserServiceImpl GetInstance호출");

return new UserServiceImpl();

}

public static UserServiceImpl init(){

System.out.println("UserServiceImpl init호출");

return new UserServiceImpl();

}

public static UserServiceImpl destroy(){

System.out.println("UserServiceImpl destroy호출");

return new UserServiceImpl();

}


<결과>


1. XML 입력 전

2014. 7. 25 오전 11:19:47 org.springframework.context.support.AbstractApplicationContext prepareRefresh

정보: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14d3343: startup date [Fri Jul 25 11:19:47 KST 2014]; root of context hierarchy

2014. 7. 25 오전 11:19:47 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

정보: Loading XML bean definitions from class path resource [user/userservice.xml]

2014. 7. 25 오전 11:19:47 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

정보: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@861f24: defining beans [userservice,vo]; root of factory hierarchy

UserServiceImpl GetInstance호출

UserServiceImpl 기본 생성자

UserServiceImpl init호출

UserServiceImpl 기본 생성자

UserVo 생성자 호출

2. XML 입력 후

addUser 호출


1. XML 입력 전

2014. 7. 25 오전 11:19:47 org.springframework.context.support.AbstractApplicationContext prepareRefresh

정보: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14d3343: startup date [Fri Jul 25 11:19:47 KST 2014]; root of context hierarchy

2014. 7. 25 오전 11:19:47 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

정보: Loading XML bean definitions from class path resource [user/userservice.xml]

2014. 7. 25 오전 11:19:47 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

정보: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@861f24: defining beans [userservice,vo]; root of factory hierarchy

UserServiceImpl GetInstance호출

UserServiceImpl 기본 생성자

UserServiceImpl init호출

UserServiceImpl 기본 생성자

UserVo 생성자 호출

2. XML 입력 후

addUser 호출


why?

destroy 객체는 호출되지 않은 이유?

- 객체 소멸 시점(자바 메모리 생성 객체 불필요하면)

- jvm에 체크만 해두고 한번에 가비지 컬렉션을 호출한다.