note

Spring @Component 어노테이션을 이용한 자동 스캔 본문

JSP/Spring

Spring @Component 어노테이션을 이용한 자동 스캔

투한 2012. 3. 2. 10:53







실행화면(Main06.java)

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







@Component라고 되어있는 객체만 오토스캔

Camera 객체를 명시한 이유는 오토 스캔 대상이 아니기 때문 


(단순 작업일 경우엔 편리하지만)
명시적으로 넘겨야될 정보가 많기 때문에 많이 사용되는 편은 아님 








applicationContext02.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">

	<!-- 자동 검색된 빈 지정 -->
	<context:component-scan base-package="madvirus.spring.chap04.homecontrol" />
	
	<bean id="camera1" class="madvirus.spring.chap04.homecontrol.Camera" p:number="1" />
	<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;

import org.springframework.stereotype.Component;

@Component("homeController")
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 + "]";
	}
	
	
}




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

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

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

		HomeController home = context.getBean("homeController",HomeController.class);
		System.out.println(home);
	}
}