note

Spring @Resource 어노테이션 본문

JSP/Spring

Spring @Resource 어노테이션

투한 2012. 3. 2. 10:11


Spring2.5부터 사용가능

스프링 싱글턴패턴

아이디 갯수만큼 객체생성





동일한 객체지만
객체주소가 다른 객체를 받기 위한
같은 프로퍼티 명의  데이터를 받는다



실행화면(Main04.java)

HomeController [alarmDevice=null, viewer=null, camera1=Camera [number=1], camera2=Camera [number=2], camera3=Camera [number=3], camera4=Camera [number=4]] 






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" />
	
</beans>



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

import javax.annotation.Resource;

public class HomeController {

	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;
	}
	@Override
	public String toString() {
		return "HomeController [alarmDevice=" + alarmDevice + ", viewer="
				+ viewer + ", camera1=" + camera1 + ", camera2=" + camera2
				+ ", camera3=" + camera3 + ", camera4=" + camera4 + "]";
	}
}




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

public interface Viewer {
	
	void add(Camera camera1);
	
	void draw();
}




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

public interface AlarmDevice {
	void alram(String name);
}




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

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

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

		HomeController home = context.getBean("homeController",HomeController.class);

		System.out.println(home);
	}
}