스프링에서 Bean 객체의 플로우를 보기 위한 예제


sayHello라는 객체를 생성하며 이 객체가 생성되고 소멸하는 라이프 사이클을 스프링의 내부 메서드들을 이용하여 확인해본다.




1) 인터페이스 MessaygeBean


package com.model.di.exam10;


public interface MessageBean {

void sayHello();


}



2) 빈 관련 인터페이스를 구현하여 빈의 사이클 주기를 체크하는 클래스

package com.model.di.exam10;

// 스프링의 빈 객체 생성 과정을 살펴보는 예제


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 {

// Bean 관련 인터페이스 4가지 구현하여 빈의 사이클 주기를 체크하기

private String beanName;

private BeanFactory beanFactory;

private String greeting;

public MessageBeanImpl() { // 라이프 사이클을 알아보기 위한 생성자

System.out.println("1. Bean의 생성자 실행");

}

public void setGreeting(String greeting) { // 위의 greeting을 받아오는 set프로퍼티 메서드

this.greeting = greeting;

System.out.println("2. setter 메서드 실행: "+greeting);

}

@Override

public void sayHello() { // 기본적인 객체 생성

System.out.println("9. "+greeting+beanName+", "+beanFactory+"!");

}


@Override

public void setBeanName(String arg0) {

this.beanName = arg0;

System.out.println("3. Bean명 지정: "+arg0);

}


@Override

public void setBeanFactory(BeanFactory arg0) throws BeansException {

this.beanFactory = arg0;

System.out.println("4. BeanFactory 지정 → "+arg0.getClass());

}


@Override // 빈 객체가 생성 완료 후 빈 객체가 호출해주는 메서드

public void afterPropertiesSet() throws Exception {

System.out.println("6. 프로퍼티 지정 완료");

}


public void init() { // init: 초기화 메서드

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

}

@Override // 빈 소멸 메서드

public void destroy() throws Exception {

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

}


}



3) 빈 인터페이스 중 초기화 전과 후에 대한 메서드를 구현하는 클래스


package com.model.di.exam10;


import org.springframework.beans.BeansException;

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


public class CustomBeanPostProcessor implements BeanPostProcessor {


@Override

public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {

// TODO Auto-generated method stub

System.out.println("5. 초기화 전 Bean에 대한 처리 실행: "+arg1);

return arg0; // 입력받은 오브젝트 타입을 반환해야 스프링이 정상 동작함.

}

@Override

public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {

System.out.println("8. 초기화 후 Bean에 대한 처리 실행: "+arg1);

return arg0;

}


}




4) 설정 파일

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="msgBean" class="com.model.di.exam10.MessageBeanImpl" init-method="init">
<property name="greeting" value="Hello, " />
</bean>

<bean id="customBean" class="com.model.di.exam10.CustomBeanPostProcessor" />

</beans>



5) 실행 결과

1. Bean의 생성자 실행
2. setter 메서드 실행: Hello, 
3. Bean명 지정: msgBean
4. BeanFactory 지정 → class org.springframework.beans.factory.support.DefaultListableBeanFactory
5. 초기화 전 Bean에 대한 처리 실행: msgBean
6. 프로퍼티 지정 완료
7. 초기화 메서드 실행
8. 초기화 후 Bean에 대한 처리 실행: msgBean
9. Hello, msgBean, org.springframework.beans.factory.support.DefaultListableBeanFactory@2acf57e3: defining beans [msgBean,customBean]; root of factory hierarchy!

10. 종료



Posted by netyhobby
,