๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

[Spring] ์Šคํ”„๋ง ๋นˆ(Bean) ์ดˆ๊ธฐํ™”, ์†Œ๋ฉธ ์‹œ ํŠน์ • ์ž‘์—…์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•

by Leica 2020. 4. 13.
๋ฐ˜์‘ํ˜•

[Spring] ์Šคํ”„๋ง ๋นˆ(Bean)์ด ์ดˆ๊ธฐํ™”, ์†Œ๋ฉธ ์‹œ ํŠน์ • ์ž‘์—…์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•

๋ฐฉ๋ฒ• 1: InitializingBean, DisposableBean ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„
๋ฐฉ๋ฒ• 2: ์Šคํ”„๋ง XML ์„ค์ • ์‚ฌ์šฉ

 

1. ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„

Spring์—์„œ ์ œ๊ณตํ•˜๋Š” InitializingBean, DisposableBean ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

public class BookDao implements InitializingBean, DisposableBean {

	@Override
	public void afterPropertiesSet() throws Exception {
		// ๋นˆ ์ดˆ๊ธฐํ™” ์‹œ ์ฝ”๋“œ ๊ตฌํ˜„
	}

	@Override
	public void destroy() throws Exception {
		// ๋นˆ ์†Œ๋ฉธ ์‹œ ์ฝ”๋“œ ๊ตฌํ˜„
	}
}

InitializingBean ์ธํ„ฐํŽ˜์ด์Šค์˜ afterPropertiesSet() ๋ฉ”์†Œ๋“œ์— ๋นˆ ๊ฐ์ฒด ์ดˆ๊ธฐํ™” ์‹œ ํ•„์š”ํ•œ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  DisposableBean ์ธํ„ฐํŽ˜์ด์Šค์˜ destroy() ๋ฉ”์†Œ๋“œ์— ๋นˆ ๊ฐ์ฒด ์†Œ๋ฉธ ์‹œ ํ•„์š”ํ•œ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

 

2. ์Šคํ”„๋ง XML ์„ค์ •

XML ์„ค์ • ํŒŒ์ผ

<bean id="bookRegisterService" class="com.brms.book.service.BookRegisterService" 
	init-method="initMethod" destroy-method="destroyMethod"/>

<bean> ํƒœ๊ทธ์˜ init-method, destroy-method ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ 1๋ฒˆ ๋ฐฉ๋ฒ•๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

init-method์— ๋นˆ ๊ฐ์ฒด ์ดˆ๊ธฐํ™” ์‹œ ํ˜ธ์ถœํ•  ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์„, destroy-method ์†์„ฑ์— ๋นˆ ๊ฐ์ฒด ์†Œ๋ฉธ ์‹œ ํ˜ธ์ถœํ•  ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์„ ์„ค์ •ํ•œ๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class BookRegisterService {

	public void initMethod() {
		// ๋นˆ ์ดˆ๊ธฐํ™” ์‹œ ์ฝ”๋“œ ๊ตฌํ˜„
	}
	
	public void destroyMethod() {
		// ๋นˆ ์†Œ๋ฉธ ์‹œ ์ฝ”๋“œ ๊ตฌํ˜„
	}
}

ํ•ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค์—์„œ init-method, destroy-method ์†์„ฑ์— ์„ค์ •ํ•œ ๊ฐ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

 

3. default-init-method์™€ default-destroy-method

์Šคํ”„๋ง XML ์„ค์ •์—์„œ <beans> ๋ ˆ๋ฒจ์— ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์†์„ฑ์ด๋‹ค.

init-method์™€ destroy-method ์†์„ฑ์ด ์„ค์ •๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ํ•ด๋‹น ๋นˆ์˜ ์ƒ์„ฑ, ์†Œ๋ฉธ ์‹œ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ˜ธ์ถœํ•  ๋ฉ”์†Œ๋“œ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

์ž๋ฐ” ์ฝ”๋“œ

public class BookRegisterService {

	public void defaultInitMethod() {
		// ๋นˆ ์ดˆ๊ธฐํ™” ์‹œ ์ฝ”๋“œ ๊ตฌํ˜„
	}
	
	public void defaultDestroyMethod() {
		// ๋นˆ ์†Œ๋ฉธ ์‹œ ์ฝ”๋“œ ๊ตฌํ˜„
	}
}

 

XML ์„ค์ •

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
       default-init-method="defaultInitMethod" default-destroy-method="defaultDestroyMethod">

<beans>์— default-init-method, default-destroy-method ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์„ ์„ค์ •ํ•œ๋‹ค.

๋ชจ๋‘ ์„ค์ •๋˜์–ด ์žˆ๋‹ค๋ฉด <bean>์˜ init-method, destroy-method๊ฐ€ ์šฐ์„ ์‹œ๋œ๋‹ค.

 

4. BeanPostProcessor

๋นˆ ๊ฐ์ฒด ์ƒ์„ฑ

โฌ‡๏ธ

[ BeanPostProcessor.postProcessBeforeInitialization ]

โฌ‡๏ธ

init-method

โฌ‡๏ธ

[ BeanPostProcessor.postProcessAfterInitialization ]

 

Spring์€ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ init-method๋กœ ์ง€์ •๋œ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๊ธฐ ์ „, ํ›„์— ์ถ”๊ฐ€์ ์ธ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•œ๋‹ค.

์ด ๊ธฐ๋Šฅ์€ BeanPostProcessor ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

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

public class TestBeanPostProcessor implements BeanPostProcessor {

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        // ์ฝ”๋“œ ๊ตฌํ˜„...
        
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // ์ฝ”๋“œ ๊ตฌํ˜„...
        
        return bean;
    }
}

๋ฉ”์†Œ๋“œ ์•„๊ทœ๋จผํŠธ๋กœ ๋นˆ ๊ฐ์ฒด์™€ ๋นˆ ์ด๋ฆ„์„ ๋ฐ›์•„์„œ ๋นˆ ๊ฐ์ฒด๋งˆ๋‹ค ๋‹ค๋ฅธ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‘ ๋ฉ”์†Œ๋“œ ๋ชจ๋‘ ๋นˆ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜์—ฌ ๋งˆ๋ฌด๋ฆฌํ•œ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€