Configuration
Annotation을 사용하여 MVC framework의 컴포넌트를 구현하기 위해서 속성 정의 XML에 다음과 같이 설정한다.
Handler 설정
@RequestMapping annotation를 처리하는 default 클래스는 다음과 같다.
- DefaultAnnotationHandlerMapping
- AnnotationMethodHandlerAdapter
위의 클래스들은 속성정의 XML에 설정해주지 않아도 자동으로 등록된다.
단, Spring에서 제공하는 HandlerMapping이나 HandlerAdapter 인터페이스를 이용하여 사용자가 새로운 Handler를 구현하는 경우에는, 반드시 사용자가 작성한 Handler와 default Handler의 설정을 속성정의 XML에 함께 표시해야한다.
예를 들어, DefaultAnnotationHandlerMapping에 Interceptor를 정의하면 모든 Request URL이 Interceptor 영향을 받게 되는데, 특정 URL에만 Interceptor를 정의하고자 하는 경우에
SelectedAnnotationHandlerMapping
을 사용하여 다음과 같이 설정할 수 있다.
common-servlet.xml
파일 예이다.
<bean id="annotationHandlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
<property name="interceptors" ref="loginInterceptor" />
</bean>
<!-- 특정URL에만 Interceptor를 적용하기 위해 사용
※ 참고 http://www.scottmurphy.info/spring_framework_annotation_based_controller_interceptors -->
<bean id="selectedAnnotationHandlerMapping"
class="org.springplugins.web.SelectedAnnotationHandlerMapping">
<!-- order 값이 작은 것이 우선적으로 적용된다. -->
<property name="order" value="0" />
<property name="urls">
<list>
<value>/updateCategory.do</value>
</list>
</property>
<property name="interceptors">
<list>
<ref bean="authorizationInterceptor" />
</list>
</property>
</bean>
Component Scan 설정
@Controller annotation으로 정의된 컨트롤러 클래스를 사용하기 위해서는
<context:component-scan/>
을 속성 정의 XML에 추가해 주어야 한다.
<context:component-scan/>에 대한 자세한 내용은 Anyframe Core 매뉴얼 >> Spring >>
Annotation
을 참고하기 바란다.
Using Filters to customize scanning
<context:component-scan/>은 해당 클래스패스 내에 @Component, @Service, @Repository, @Controller annotation 이 적용된 클래스를 모두 찾아서 Spring 컨테이너가 관리하는 컴포넌트로 등록하도록 하는 설정이다.
이와 같은 디폴트 설정으로 stereotype annotation을 Auto Detecting하여 사용 시, 다음과 같은 문제가 발생할 수 있다.
Resources
다운로드
이클립스 프로젝트 형태의 샘플 웹 어플리케이션을 포함하고 있는 anyframe-springmvc-sample-annotation.zip 파일을 다운받은 후, 테스트 환경 설정
을 참조하여
위에서 제시한 예제 코드를 실행해 볼 수 있다.
| Name |
Download |
| anyframe-springmvc-sample-annotation.zip |
Download
|
참고자료