Customize Bean
Spring Framework๋ Bean์ ํน์ฑ์ ๋ง์ถค ์ค์ ํ๊ธฐ ์ํด ์ฌ์ฉํ ์ ์๋ ์ฌ๋ฌ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํฉ๋๋ค. ์ด ์น์ ์์๋ ์ด๋ฅผ ๋ช ๊ฐ์ง๋ก ๊ทธ๋ฃนํํ์ฌ ์ค๋ช ํฉ๋๋ค:
Lifecycle Callbacks (์๋ช ์ฃผ๊ธฐ ์ฝ๋ฐฑ)
ApplicationContextAware ๋ฐ BeanNameAware
๊ธฐํ Aware ์ธํฐํ์ด์ค
Lifecycle Callbacks
Spring ์ปจํ
์ด๋์์ ๊ด๋ฆฌํ๋ Bean์ ์๋ช
์ฃผ๊ธฐ(lifecycle)์ ์ํธ ์์ฉํ๋ ค๋ฉด, InitializingBean
๊ณผ DisposableBean
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ์ ์์ต๋๋ค.
InitializingBean
์ธํฐํ์ด์ค๋afterPropertiesSet()
๋ฉ์๋๋ฅผ ์ ๊ณตํ์ฌ Bean์ด ์ด๊ธฐํ ์์ ์ ํ ์ ์๋๋ก ํฉ๋๋ค.DisposableBean
์ธํฐํ์ด์ค๋destroy()
๋ฉ์๋๋ฅผ ํตํด Bean์ด ํ๊ดด๋ ๋ ์์ ์ ์ํํ ์ ์์ต๋๋ค.
@PostConstruct
์ @PreDestroy
์ ๋
ธํ
์ด์
์ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ชจ๋ํ Spring ์ ํ๋ฆฌ์ผ์ด์
์์ ์๋ช
์ฃผ๊ธฐ ์ฝ๋ฐฑ์ ๋ฐ๋ ์ต์ ์ ๋ฐฉ๋ฒ์ผ๋ก ๊ถ์ฅ๋ฉ๋๋ค. ์ด๋ฅผ ์ฌ์ฉํ๋ฉด Bean์ด Spring์ ์ข
์๋์ง ์์ผ๋ฏ๋ก ๋ ์ ์ฐํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
InitializingBean
Spring์์ ์ด๊ธฐํ ์์
์ ์ํํ๋ ค๋ฉด InitializingBean
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ , afterPropertiesSet()
๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ํฉ๋๋ค. ์ด ๋ฉ์๋๋ Spring ์ปจํ
์ด๋๊ฐ ํด๋น Bean์ ํ์ํ ๋ชจ๋ ์์ฑ์ ์ค์ ํ ํ์ ํธ์ถ๋ฉ๋๋ค.
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
public class ExampleBean {
public void init() {
// Bean ์ด๊ธฐํ ์์
์ํ
}
}
์์ ์์๋ InitializingBean
์ ์ง์ ๊ตฌํํ ์์์ ๊ฑฐ์ ๋์ผํ ์ญํ ์ ํฉ๋๋ค.
public class AnotherExampleBean implements InitializingBean {
@Override
public void afterPropertiesSet() {
// Bean ์ด๊ธฐํ ์์
์ํ
}
}
์ฃผ์:
@PostConstruct
์ ์ด๊ธฐํ ๋ฉ์๋๋ Bean์ด ์์ ํ ์ด๊ธฐํ๋ ํ์ ์คํ๋ฉ๋๋ค. ์ด ๋จ๊ณ์์๋ ์ธ๋ถ Bean๊ณผ์ ์ํธ์์ฉ์ ํ๊ธฐ ์ ์ ๋ด๋ถ์ ์ธ ์ด๊ธฐํ๋ง ์ํํด์ผ ํ๋ฉฐ, ๊ทธ๋ ์ง ์์ผ๋ฉด **์ด๊ธฐํ ๊ต์ฐฉ ์ํ(deadlock)**๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค.
๋น์ฉ์ด ๋ง์ด ๋๋ ํ์ ์ด๊ธฐํ ์์
๋น์ฉ์ด ๋ง์ด ๋๋ ์ด๊ธฐํ ์์
(์: ๋น๋๊ธฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค๋น)์ ํธ๋ฆฌ๊ฑฐํด์ผ ํ ๊ฒฝ์ฐ์๋ SmartInitializingSingleton.afterSingletonsInstantiated()
๋ฉ์๋๋ฅผ ๊ตฌํํ๊ฑฐ๋ ContextRefreshedEvent
๋ฅผ ์ฒ๋ฆฌํ๋ ApplicationListener๋ฅผ ๊ตฌํํ์ฌ ๋ชจ๋ ์ฑ๊ธํค ์ด๊ธฐํ๊ฐ ๋๋ ํ์ ์์
์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
DisposableBean
Bean์ด ํ๊ดด๋ ๋(์ ํ๋ฆฌ์ผ์ด์
์ข
๋ฃ ์ ๋ฑ) ํน์ ์์
์ ์ํํ๋ ค๋ฉด, DisposableBean
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ , destroy()
๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ํ์ฌ ์์
์ ์ ์ํ ์ ์์ต๋๋ค.
<bean id="exampleDestructionBean" class="examples.ExampleBean" destroy-method="cleanup"/>
public class ExampleBean {
public void cleanup() {
// Bean ํ๊ดด ์์
์ํ (์: ๋ฆฌ์์ค ํด์ )
}
}
์ด ์์๋ DisposableBean
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๋ค์์ ์์์ ๋์ผํ ์ญํ ์ ํฉ๋๋ค:
public class AnotherExampleBean implements DisposableBean {
@Override
public void destroy() {
// Bean ํ๊ดด ์์
์ํ
}
}
์ฃผ์: Bean์ ํ๊ดดํ ๋๋
@PreDestroy
์ ๋ ธํ ์ด์ ์ด๋destroy-method
์์ฑ์ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ถ์ฅ๋๋ฉฐ, Spring์ ์ข ์๋์ง ์๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ ์ ์์ต๋๋ค.
Default Initialization and Destroy Methods
๊ธฐ๋ณธ ์ด๊ธฐํ/ํ๊ดด ๋ฉ์๋ ์ค์ : Spring์์๋ Bean์ ๋ํด ์ผ๊ด๋ ๋ผ์ดํ์ฌ์ดํด ์ฝ๋ฐฑ ๋ฉ์๋ ์ด๋ฆ์ ์ง์ ํ ์ ์์ต๋๋ค.
์๋ฅผ ๋ค์ด, ๋ชจ๋ Bean์์ ์ด๊ธฐํ ๋ฉ์๋๊ฐ init()
์ด๊ณ , ํ๊ดด ๋ฉ์๋๊ฐ destroy()
๋ผ๋ฉด, ๊ฐ Bean ์ ์๋ง๋ค ๋ฉ์๋๋ฅผ ๋ช
์ํ ํ์ ์์ด default-init-method
์ default-destroy-method
์์ฑ์ ์ฌ์ฉํ์ฌ ์ด๋ฅผ ์ ์ญ์ผ๋ก ์ ์ฉํ ์ ์์ต๋๋ค.
<beans default-init-method="init" default-destroy-method="destroy">
<bean id="myService" class="com.example.MyService"/>
</beans>
์ด ๊ฒฝ์ฐ, MyService
ํด๋์ค์ init()
๊ณผ destroy()
๋ฉ์๋๊ฐ ์๋ค๋ฉด, Bean์ด ์์ฑ๋๊ฑฐ๋ ํ๊ดด๋ ๋ ์๋์ผ๋ก ํธ์ถ๋ฉ๋๋ค.
ApplicationContextAware ๋ฐ BeanNameAware
ApplicationContextAware: Bean์ด ApplicationContext๋ฅผ ์ฐธ์กฐํ๋๋ก ์ค์ ํ๋ ค๋ฉด ApplicationContextAware
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์ฌ setApplicationContext()
๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ํฉ๋๋ค.
public class MyBean implements ApplicationContextAware {
private ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
}
BeanNameAware: Bean์ด ์์ ์ ์ด๋ฆ์ ์ฐธ์กฐํ๋๋ก ์ค์ ํ๋ ค๋ฉด
BeanNameAware
์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์ฌsetBeanName()
๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ํฉ๋๋ค.public class MyBean implements BeanNameAware { @Override public void setBeanName(String name) { System.out.println("Bean name: " + name); } }
์ด ์ธํฐํ์ด์ค๋ค์ Bean์ด ์์ ์ ์์ฑํ ์ปจํ ์ด๋๋ ์์ ์ ๋ํ ์ ๋ณด๋ฅผ ์ฐธ์กฐํ ์ ์๊ฒ ํด์ค๋๋ค.
3. ๊ธฐํ Aware ์ธํฐํ์ด์ค
Spring์ ์ถ๊ฐ์ ์ผ๋ก ๋ค์ํ Aware ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ์ฌ Bean์ด ํ์ํ ์ธํ๋ผ ์ข ์์ฑ์ ์ปจํ ์ด๋๋ก๋ถํฐ ์ ๊ณต๋ฐ์ ์ ์๋๋ก ํฉ๋๋ค. ๋ํ์ ์ธ Aware ์ธํฐํ์ด์ค๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
์ธํฐํ์ด์ค
์ฃผ์ ๋๋ ์์กด์ฑ
์ค๋ช
ApplicationContextAware
ApplicationContext
์ปจํ ์ด๋๋ฅผ ์ฐธ์กฐํฉ๋๋ค.
BeanClassLoaderAware
Bean ํด๋์ค ๋ก๋
Bean ํด๋์ค ๋ก๋๋ฅผ ์ฐธ์กฐํฉ๋๋ค.
BeanFactoryAware
BeanFactory
BeanFactory๋ฅผ ์ฐธ์กฐํฉ๋๋ค.
ResourceLoaderAware
ResourceLoader
๋ฆฌ์์ค๋ฅผ ์ฐธ์กฐํ๋ ๋ก๋์ ๋๋ค.
MessageSourceAware
MessageSource
๋ฉ์์ง ํด๊ฒฐ ์ ๋ต์ ์ฃผ์ ๋ฐ์ต๋๋ค.
ServletContextAware
ServletContext (์น ์ ํ๋ฆฌ์ผ์ด์ ์์)
ํ์ฌ ServletContext๋ฅผ ์ฐธ์กฐํฉ๋๋ค.
์ด ์ธํฐํ์ด์ค๋ค์ ์ฃผ๋ก ์ธํ๋ผ์ ๊ฐ๊น์ด Bean์์ ์ฌ์ฉ๋๋ฉฐ, ์ผ๋ฐ ์ ํ๋ฆฌ์ผ์ด์ ์ฝ๋์์๋ Spring์ ๋ํ ์์กด์ฑ์ ์ค์ด๊ธฐ ์ํด ํผํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
Lifecycle ์ธํฐํ์ด์ค๋ฅผ ํ์ฉํ Startup ๋ฐ Shutdown ์ฝ๋ฐฑ
Spring์์๋ Lifecycle ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์ฌ Bean์ด ์ ํ๋ฆฌ์ผ์ด์ ์ปจํ ์คํธ๊ฐ ์์๋๊ฑฐ๋ ์ข ๋ฃ๋ ๋ ์๋์ผ๋ก ์์/์ข ๋ฃ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค.
public interface Lifecycle {
void start();
void stop();
boolean isRunning();
}
์ด ์ธํฐํ์ด์ค๋ ์ ํ๋ฆฌ์ผ์ด์ ์ด ์์๋๊ฑฐ๋ ์ค์ง๋ ๋ ํน์ ์์ ์ ์ํํด์ผ ํ๋ ๊ฐ์ฒด์ ์ ํฉํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์คํ๋๋ ์๋น์ค๊ฐ ์์ ๋ ์ ์ฉํฉ๋๋ค.
SmartLifecycle ์ธํฐํ์ด์ค๋
Lifecycle
์ ํ์ฅํ๋ฉฐ, ๋ ์ธ๋ฐํ๊ฒ ์ ์ดํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. ์๋ฅผ ๋ค์ด,getPhase()
๋ฉ์๋๋ฅผ ํตํด Bean์ด ์์๋๊ฑฐ๋ ์ข ๋ฃ๋ ์์๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค.
public interface SmartLifecycle extends Lifecycle, Phased {
boolean isAutoStartup();
void stop(Runnable callback);
}
getPhase()
๋ฉ์๋๋ ์์ ์์์ ์ข
๋ฃ ์์๋ฅผ ์ง์ ํ ๋ ์ฌ์ฉ๋๋ฉฐ, ๊ฐ์ด ๋ฎ์์๋ก ๋จผ์ ์์๋๊ณ ๋์ค์ ์ข
๋ฃ๋ฉ๋๋ค.
Last updated