Spring/Study
스프링(Spring) 생명주기(Life Cycle)
DevMoomin
2020. 4. 13. 15:25
(공감과 댓글 하나는 글쓴이에게 큰 힘이 됩니다.)
스프링 컨테이너가 생성되면 컨테이터 내부에서 빈(Bean) 객체가 생성되고, 빈(Bean) 객체들은 서로 의존 관계를 가지고 있다. 따라서 스프링 컨테이너와 빈(Bean) 객체의 생명주기는 동일하다.
인터페이스(Interface)를 이용하는 방법
BookDao.java
public class BookDao implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("빈(Bean)객체 생성 단계");
}
@Override
public void destroy() throws Exception {
System.out.println("빈(Bean)객체 소멸 단계");
}
}
|
cs |
init-method, destroy-method 속성을 이용하는 방법
appCtx.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">
<context:annotation-config />
<bean id="bookRegisterService" class="com.brms.book.service.BookRegisterService"
init-method="initMethod" destroy-method="destroyMethod"/>
</beans>
|
cs |
BookRegisterService.java
public class BookRegisterService {
public void initMethod() {
System.out.println("BookRegisterService 빈(Bean)객체 생성 단계");
}
public void destroyMethod() {
System.out.println("BookRegisterService 빈(Bean)객체 소멸 단계");
}
}
|