일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- docker
- Python
- helm
- JavaScript
- Vision
- Trino
- kubectl
- CSV
- Spring
- kubeadm
- zookeeper
- ip
- aws s3
- log
- jvm
- Packet
- grafana
- MAC address
- kubernetes
- java
- OS
- tcp
- AWS
- CVAT
- Operating System
- PostgreSQL
- airflow
- Kafka
- EC2
- Network
- Today
- Total
JUST WRITE
Bean LifeCycle 본문
Bean LifeCycle
Spring Bean을 아래와 같은 일정한 LifeCycle을 가진다.
Spring Container 생성 -> Spring Bean 생성 -> DI 주입 -> 초기화 Callback -> Bean 사용 -> 소멸 전 Callback -> 소멸
Spring Container는 Spring Bean Factory로서 Bean들의 생성, 소멸 등 LifeCycle를 관리한다.
Spring에서는 Bean LifeCycle에서 Custom 할 수 있는 2가지 Callback Method를 제공한다.
- Post-initalization call back methods
- Pre-destruction call back methods
LifeCycle Callback Methods
InitializingBean and DisposableBean
InitializingBean Interface는 Spring Container에서 Bean의 필요한 모든 속성을 세팅 후 초기화 작업을 한다.
DisposableBean Interface는 Spring Container가 destroy될때 호출된다.
package com.howtodoinjava.task;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class DemoBean implements InitializingBean, DisposableBean
{
//Other bean attributes and methods
@Override
public void afterPropertiesSet() throws Exception
{
//Bean initialization code
}
@Override
public void destroy() throws Exception
{
//Bean destruction code
}
}
위 interface를 구현하는 것은 초기에 나온 방법이라 지금은 init-method 방식이 선호된다.
init() and destroy() methods
init와 destory Method은 아래 2가지 방식으로 적용할 수 있다.
- Bean별로 적용하는 방식
- Beans Context에 속한 Bean에 Global 하게 적용하는 방식
Local, Bean별로 적용
<beans>
<bean id="demoBean" class="com.howtodoinjava.task.DemoBean"
init-method="customInit"
destroy-method="customDestroy"></bean>
</beans>
Global, Benas Context 적용
<!-- <beans> tag내 bean 적용 -->
<beans default-init-method="customInit" default-destroy-method="customDestroy">
<bean id="demoBean" class="com.howtodoinjava.task.DemoBean"></bean>
</beans>
위처럼 Spring Container에 정의했으면, Bean에 해당 Method를 정의한다.
package com.howtodoinjava.task;
public class DemoBean
{
public void customInit()
{
System.out.println("Method customInit() invoked...");
}
public void customDestroy()
{
System.out.println("Method customDestroy() invoked...");
}
}
@PostConstruct and @PreDestroy
Spring 2.5부터 이 Annotation 방식이 지원되었다.
@PostConstruct Method는 기본 Constructor로 Bean 생성 후 Bean이 반환되기 직전에 실행된다.
@PreDestroy Method는 Spring Container에 의해 Bean 소멸 전에 실행된다.
public class DemoBean
{
@PostConstruct
public void customInit()
{
System.out.println("Method customInit() invoked...");
}
@PreDestroy
public void customDestroy()
{
System.out.println("Method customDestroy() invoked...");
}
}
[참고사이트]
Spring - Bean Life Cycle Explained - HowToDoInJava
Learn about Spring bean life cycle. We will learn about life cycle stages, initialization and destroy call back methods and how to control life cycle events.
howtodoinjava.com
[Spring] Bean LifeCycle 이란 무엇일까?
빈 생명주기 콜백(Bean LifeCycle) 데이터베이스 커넥션 풀이나, 네트워크 소켓처럼 애플리케이션 시작 시점에 필요한 연결을 미리 해두고, 애플리케이션 종료 시점에 연결을 모두 종료하는 작업을
devlog-wjdrbs96.tistory.com
'Programing > Spring' 카테고리의 다른 글
MappingJackson2JsonView (0) | 2021.10.09 |
---|---|
Singleton Registry (0) | 2021.10.02 |
Spring Bean Scopes (0) | 2021.09.28 |
Spring Bean (0) | 2021.09.26 |
IoC / DI (0) | 2021.09.26 |