Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
Tags
- Java
- struts2
- 배열
- Spring
- paint
- JavaScript
- Android
- 안드로이드
- Graphic
- 클래스
- 국제화
- layout
- oracle
- 예외처리
- mybatis
- JSP
- 메서드
- 생성자
- OGNL
- 기본
- 오버로딩
- 메소드
- AWT
- 어노테이션
- 이클립스
- Menu
- 에러페이지
- Eclips
- HTML
- 전화걸기
Archives
- Today
- Total
note
Spring @Required 어노테이션(Annotation) 본문
전제 조건
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 /> 로 대처해도 실행화면은 같다
Camera [number=1]
※※※※※※※※※※※
-RequiredAnnotationBeanPostProcessor :@Required 사용시
-AutowiredAnnotationBeanPostProcessor : @Autowired 사용시
-CommonAnnotationBeanPostProcessor : @Resource,@PostConstruct , @PreDestroy 사용시
-ConfigurationClassPostProcessor : @Configuration 사용시
<context:annotation-config />
로 모두 대처 가능함
<context:annotation-config />
로 모두 대처 가능함
'JSP > Spring' 카테고리의 다른 글
| Spring @Qualfier 어노테이션 (0) | 2012.02.29 |
|---|---|
| Spring @Autowired 어노테이션 (0) | 2012.02.29 |
| Spring 국제화 (0) | 2012.02.29 |
| Spring 프로퍼티 타입을 이용한 의존 관계 자동 설정 (0) | 2012.02.29 |
| Spring 프로퍼티 이름을 이용한 의존 관계 자동 설정 (0) | 2012.02.29 |