note

Spring @PostConstruct / @PreDestroy 어노테이션과 라이프 사이클 본문

JSP/Spring

Spring @PostConstruct / @PreDestroy 어노테이션과 라이프 사이클

투한 2012. 3. 2. 10:32






[JSP/Spring] - Spring @Resource 어노테이션

이전 게시물과 연관 있습니다




실행 화면(Main05.java)

init 메소드 동작

close 메소드 동작







기존

ApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
  

AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocations);



추가

context.registerShutdownHook();
작업이 끝난후 메모리 정리 



라이프 사이클을 만들 수 있지만
많이 사용하는 방법은 아님





applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	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-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<!-- 
	-RequiredAnnotationBeanPostProcessor  :@Required 사용시
	-AutowiredAnnotationBeanPostProcessor : @Autowired 사용시
	-CommonAnnotationBeanPostProcessor : @Resource,@PostConstruct , @PreDestroy 사용시
	-ConfigurationClassPostProcessor : @Configuration 사용시 
	
	아래 태그로 대체 가능
	<context:annotation-config /> 
	-->
	
	<!-- @Required 어노테이션 사용시 필수적으로 설정 -->
	<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />
		
	<bean id="camera1" class="madvirus.spring.chap04.homecontrol.Camera">
	
		<!-- number 프로퍼티에 @Required 어노테이션적용
		number 프로퍼티를 설정하지 않을 경우 예외 발생 -->
		<property name="number" value="1" />
	</bean>
	

	
	<!-- @Resource 어노테이션 사용시 필수적으로 설정 -->
	<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

	<bean id="homeController" class="madvirus.spring.chap04.homecontrol.HomeController" />
	<bean id="camera2" class="madvirus.spring.chap04.homecontrol.Camera" p:number="2" />
	<bean id="camera3" class="madvirus.spring.chap04.homecontrol.Camera" p:number="3" />
	<bean id="camera4" class="madvirus.spring.chap04.homecontrol.Camera" p:number="4" />
	
	
	<!-- @PostConstruct 어노테이션 및 @PreDestroy 어노테이션 사용 -->
	<bean id="homeController2" class="madvirus.spring.chap04.homecontrol.HomeController2" />
	
	
</beans>




HomeController2.java
package madvirus.spring.chap04.homecontrol;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

public class HomeController2 {

	private AlarmDevice alarmDevice;
	private Viewer viewer;

	@Resource(name = "camera1")
	private Camera camera1;

	@Resource(name = "camera2")
	private Camera camera2;

	@Resource(name = "camera3")
	private Camera camera3;

	private Camera camera4;

	
	
	public void setCamera1(Camera camera1) {
		this.camera1 = camera1;
	}
	public void setCamera2(Camera camera2) {
		this.camera2 = camera2;
	} 
	public void setCamera3(Camera camera3) {
		this.camera3 = camera3;
	}
	@Resource(name = "camera4")
	public void setCamera4(Camera camera4) {
		this.camera4 = camera4;
	}
	
	@PostConstruct
	public void init(){
		System.out.println("init 메소드 동작");
	}
	
	@PreDestroy
	public void close(){
		System.out.println("close 메소드 동작");
	}
}




Main05.java
package madvirus.spring.chap04.homecontrol;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main05 {
	public static void main(String[] args) {
		String[] configLocations = new String[] { "applicationContext.xml" };
		AbstractApplicationContext context = new ClassPathXmlApplicationContext(
				configLocations);
		context.registerShutdownHook();

		HomeController2 home = context.getBean("homeController2",HomeController2.class);
	}
}





'JSP > Spring' 카테고리의 다른 글

Spring AOP  (1) 2012.03.02
Spring @Component 어노테이션을 이용한 자동 스캔  (0) 2012.03.02
Spring @Resource 어노테이션  (0) 2012.03.02
Spring @Qualfier 어노테이션  (0) 2012.02.29
Spring @Autowired 어노테이션  (0) 2012.02.29