728x90
반응형

Spring 은 경량 컨테이너 이다. 

컨테이너는 컴포넌트들의 라이프사이클을 관리하는 걸 말하는데 예를 들어 톰캣,스윙(JPanel) 등...

Spring도 마찬가지로 초기화작업이나 소멸시에 작동할 작업을 설정을 통해 미리 지정해주는것이 가능하다. 


LifeCycleBean.java

package test.life;

public class LifeCycleBean {

public void init() {

System.out.println("초기화 메소드 입니다.");

}

public void execute() {

System.out.println("요청 작업을 수행합니다.");

}

public void destroy() {

System.out.println("객체가 소멸될때 호출되는 메소드입니다.");

}

}


test01.xml

<?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">

<!-- 

객체가 생성될떄 init 메소드가 호출되고 객체가 소멸될때 destroy 메소드가 호출된다. 

init-method : 객체 생성시 호출되는 메소드 지정 (초기화작업)

destroy-method : 객체소멸시 호출되는 메소드 (자원해제등의 작업)

-->

<bean name="life" class="test.life.LifeCycleBean"

init-method="init" destroy-method="destroy"

/>

</beans>



scope 속성을 이용하여 LifeCycle 을 관리할 수 있다. 

init-method : 객체 생성시 호출되는 메소드 지정 (초기화작업)

destroy-method : 객체소멸시 호출되는 메소드 (자원해제등의 작업)


728x90
반응형

+ Recent posts