note

Spring @Required 어노테이션(Annotation) 본문

JSP/Spring

Spring @Required 어노테이션(Annotation)

투한 2012. 2. 29. 16:25











전제 조건
 applicationContext.xml 에

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />


 







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>

</beans>

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

import org.springframework.beans.factory.annotation.Required;

public class Camera {

	private int number;

	public Camera(){
	}
	
	@Required
	public void setNumber(int number) {
		this.number = number;
	}
	@Override
	public String toString() {
		return "Camera [number=" + number + "]";
	}	
}



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

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

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

		Camera camera = context.getBean("camera1", Camera.class);
		System.out.println(camera);
	}
}








Camera.java

setNumber에 반드시 데이터를 넘기라는 표현
Spring에서만 사용할 수 있는 방법 





짧은 표현 방법

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />

<context:annotation-config /> 로 대처해도 실행화면은 같다








실행 화면 (Main.java)

Camera [number=1]

 




※※※※※※※※※※※

-RequiredAnnotationBeanPostProcessor  :@Required 사용시
-AutowiredAnnotationBeanPostProcessor : @Autowired 사용시
-CommonAnnotationBeanPostProcessor : @Resource,@PostConstruct , @PreDestroy 사용시
-ConfigurationClassPostProcessor : @Configuration 사용시

<context:annotation-config />

로 모두 대처 가능함