관리 메뉴

IT.FARMER

Spring Annotation 사용방법 본문

Spring

Spring Annotation 사용방법

아이티.파머 2011. 3. 14. 19:17
반응형
@Component , @Controller , @Service , @Repository 
일반 Bean        콘트롤러        메니저        다오


여기저기 찾아보고 읽어본결과  이렇게 사용되는것 같아 보인다.
예전에 EJB3 를 하이버넷과 함께 사용할때와 같이 Class위에 위의 어노테이션을 사용한다.

서버시작과 함께 빈들을 참조하고 올리기 시작하는데.
위와 같이 어노테이션을 사용함은 주입식 으로변화 되었기 때문이라고 어디선가 본듯.....?

아무튼, 이 기호를 클레스위에 선헌한뒤 XML에는 어떻게 바꿔줘야 할까?
@Controller
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
<property name="alwaysUseFullPath" value="true"></property>
</bean>

1. CommonAnnotationBeanPostProcessor      : Class안의 @Resource , ....등 로딩
2. AutowiredAnnotationBeanPostProcessor     : Class안의 @Autowired, ....등 로딩
3. AnnotationMethodHandlerAdapter               : Class안의 @RequestMapping 로딩
4. DefaultAnnotationHandlerMapping               : 

@Component ,
특별한 의미 없음, 현재 클래스를 Bean으로 등록하여 사용하겠다는것임.
Ex)스프링에서 beanName을 대부분 클레스명의 첫글자인 대문자를 소문자로 변경하여 사용하게 되는데.
@Component를 사용하여
@Controller 
단순히 현재 클레스가 Controller이라는것을 명시한다. 
@Service 
설명...
@Repository 
설명....

어노테시연을 사용 하는 방법은 다음에.



마지막으로, 위와 같이 사용하지 않고 어노테이션을 사용한 Bean들을 한번에 찾아 로딩 시켜주는게있다.
<context:component-scan base-package="com.dongbu.farm.common" />
<context:annotation-config />

 <context:component-scan /> <context:annotation-config />  : 

1.  서비스 속성 정의 XML 내에 <context:component-scan /> 을 정의함으로써 Spring 컨테이너가 Stereotype Annotation을 정의한 클래스를 자동 검색할 수 있도록 한다. 

2. <context:component-scan />을 정의한 경우 Annotation 인식을 위한 설정 <context:annotation-config/> 을 별도로 추가하지 않아도 된다.  <context:annotation-config></context:annotation-config>
 
반응형

'Spring' 카테고리의 다른 글

Spring @annotation 사용시 주의점 및 사용 방법 ?? 수정중,.,..  (0) 2011.11.17
spring annotation  (0) 2011.03.30
Spring Quartz 1-2  (0) 2010.12.14
Spring Quartz 1-1  (0) 2010.12.14
Spring Timer Sample  (0) 2010.12.14