본문 바로가기

프로그래밍/SPRING 2.5

스프링 라이프 사이클 확인하기.


1. 스프링 컨테이너의 라이프 사이클..대표적으로 두가지가 있다.


BeanFactory와 ApplicationContext




위의 내용을 확인해자.


프로젝트 루트 아래 적당한 패키지를 만들고 파일을 구성해보자.


1. MessageBean 인터페이스


package myspring.sample3;


public interface MessageBean {

void sayHello();

}


2. MessageBeanImpl 클래스

아래는 스프링 컨테이너가 구동되는 원리를 살펴보기 위해서 MessageBean, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean 인터페이스를 구현하였다.

(위의 그림 순서 참조)


package myspring.sample3;


import org.springframework.beans.BeansException;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.BeanFactoryAware;

import org.springframework.beans.factory.BeanNameAware;

import org.springframework.beans.factory.DisposableBean;

import org.springframework.beans.factory.InitializingBean;


public class MessageBeanImpl implements MessageBean, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {


private String greeting;

private String beanName;

private BeanFactory factory;

//Constructor

public MessageBeanImpl(){

System.out.println("1. 인스턴스화");

}

//Setter Method

public void setGreeting(String greeting) {

this.greeting = greeting;

System.out.println("2. Property 할당");

}


@Override

public void setBeanName(String beanName) {

// TODO Auto-generated method stub

System.out.println("3. BeanNameAware (빈의 이름 설정)");

this.beanName = beanName;

System.out.println("빈의 이름 : " + beanName);

}

@Override

public void setBeanFactory(BeanFactory factory) throws BeansException {

// TODO Auto-generated method stub

System.out.println("4. BeanFactoryAware (빈 팩토리 설정)");

this.factory = factory;

System.out.println("빈 팩토리 : " + factory.getClass());

}

@Override

public void afterPropertiesSet() throws Exception {

// TODO Auto-generated method stub

System.out.println("6. Property 설정완료");

}


public void start(){

System.out.println("7. 초기화 메서드 실행");

}

@Override

public void destroy() throws Exception {

// TODO Auto-generated method stub

System.out.println("종료");

}

@Override

public void sayHello() {

// TODO Auto-generated method stub

System.out.println("마지막. 인사말 : " + greeting + " " + beanName + " !");

}

}



3. BeanPostProcessorImpl  클래스


package myspring.sample3;


import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanPostProcessor;


public class BeanPostProcessorImpl implements BeanPostProcessor {


@Override

public Object postProcessAfterInitialization(Object arg0, String arg1)

throws BeansException {

// TODO Auto-generated method stub

System.out.println("8. 초기화 후의 빈에 대한 처리를 실행");

return arg0;

}


@Override

public Object postProcessBeforeInitialization(Object arg0, String arg1)

throws BeansException {

// TODO Auto-generated method stub

System.out.println("5. 초기화 전의 빈에 대한 처리를 실행");

return arg0;

}

}




4. applicationContext.xml 파일


<?xml version="1.0" encoding="UTF-8"?>


<beans:beans xmlns="http://www.springframework.org/schema/mvc"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:beans="http://www.springframework.org/schema/beans"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    

    <beans:bean id="msgBean" class="myspring.sample3.MessageBeanImpl" init-method="start">

        <beans:property name="greeting">

            <beans:value>Hello everybody!!</beans:value>

        </beans:property>

    </beans:bean>

   

</beans:beans>



5. 실행 클래스


package myspring.sample3;


import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;


public class HelloApp {


public static void main(String[] args) {

// TODO Auto-generated method stub

Resource rs = new ClassPathResource("applicationContext3.xml");

XmlBeanFactory factory = new XmlBeanFactory(rs);

factory.addBeanPostProcessor(new BeanPostProcessorImpl());

MessageBean msg = (MessageBean)factory.getBean("msgBean");

msg.sayHello();

}

}


<<<결과>>>
1. 인스턴스화
2. Property 할당
3. BeanNameAware (빈의 이름 설정)
빈의 이름 : msgBean
4. BeanFactoryAware (빈 팩토리 설정)
빈 팩토리 : class org.springframework.beans.factory.xml.XmlBeanFactory
5. 초기화 전의 빈에 대한 처리를 실행
6. Property 설정완료
7. 초기화 메서드 실행
8. 초기화 후의 빈에 대한 처리를 실행
마지막. 인사말 : Hello everybody!! msgBean !


위의 소스는 BeanFactory를 이용한 것이고 아래는 applicationContext를 이용한때의 구동 순서이다.

차이는 거의 없다 (위의 그림 참조) 거의다 같은 소스

아래 파일만 바꿔주면 된다.

여기서는 ApplicationContext클래스를 사용하였기때문에 BeanPostProcessorImpl에 대한 내용을 설정파일에서 설정해주어야 한다.


1. applicationContext4.xml


<?xml version="1.0" encoding="UTF-8"?>


<beans:beans xmlns="http://www.springframework.org/schema/mvc"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:beans="http://www.springframework.org/schema/beans"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    

    <beans:bean id="msgBean" class="myspring.sample3.MessageBeanImpl" init-method="start">

        <beans:property name="greeting">

            <beans:value>Hello everybody!!</beans:value>

        </beans:property>

    </beans:bean>

    

    <beans:bean class="myspring.sample3.BeanPostProcessorImpl" />

   

</beans:beans>



2. 실행파일



package myspring.sample3;


import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;


public class HelloApp2 {


public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext ctx;

ctx = new FileSystemXmlApplicationContext("applicationContext4.xml");

MessageBean msg = (MessageBean)ctx.getBean("msgBean");

msg.sayHello();

}

}



<<<결과>>>


1. 인스턴스화

2. Property 할당

3. BeanNameAware (빈의 이름 설정)

빈의 이름 : msgBean

4. BeanFactoryAware (빈 팩토리 설정)

빈 팩토리 : class org.springframework.beans.factory.support.DefaultListableBeanFactory

5. 초기화 전의 빈에 대한 처리를 실행

6. Property 설정완료

7. 초기화 메서드 실행

8. 초기화 후의 빈에 대한 처리를 실행

마지막. 인사말 : Hello everybody!! msgBean !









'프로그래밍 > SPRING 2.5' 카테고리의 다른 글

간단한 게시판 만들기 - 리스트 페이지  (0) 2013.09.05
스프링의 구조  (0) 2013.09.05
스프링 컨트롤러 계층 구조  (0) 2013.09.05
DispatcherServlet  (0) 2013.09.04
스프링 MVC 라이프 사이클  (0) 2013.09.04