스프링 2.5 부터는 클래스 패스에 위치한 클래스를 검색하여 특정한 어노테이션이 붙은 클래스를 자동으로 빈으로 등록하는 
기능을 제공하고 있다.



스프링 2.0 에서는 @Repository ,  2.5 에서는 @Component , @Service , @Controller 가 추가되었다.



@Component 어노테이션을 해당 클래스에 적용하면?
<context:component-scan> 태그를 이용해서 검색할 패키지를 지정하면 된다.

그러면 지정한 패키지에서 찾아서 빈으로 등록한다. 

<context:component-scan base-package="xxx.xxxx.xxx.home" />

 
자동 검색된 빈의 이름은 클래스의 이름을 빈의 이름으로 사용한다. (첫글자는 소문자로 바꾼뒤)

HomeController 라는 클래스였다면 빈 이름은 homeController 이다.
물론 이름을 지정할 수 도 있다. ("homeController")라고 어노테이션 옆에 적어주면된다.

해당 어노테이션을 설정할때 생성될 빈의 범위를 지정할 수 도 있다. @Scope("prototype") 이런식으로...


스캔 대상 클래스범위를 지정할 수 있는데...
<context:include-filter> 와 <context:exclude-filter> 를 이용하면 포함과 비포함 대상을 명시할 수 있다.

e.g

<context:component-scan baase-package="xxxx.xxx.xxxx.xxx">
  <context:include-filter type="regex" expression=".*HibernateRepository"/>
  <context:exclude-filter type="aspectj" expression="..*IBatisRepository"/>
</context:component-scan>

위의 필터 타입의 속성은
regex : 정규식 매칭 대상
aspectj : AspectJ 표현식으로 ...
annotation : 특정 어노테이션이 적용된...
assignable : 지정한 타입으로 할당되는...

e.g)
 
<context:component-scan base-package="com.sib" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation" expression="com.sib.xxx.model.annotation.Model" />
</context:component-scan>
-------------------------

위의것은 자동스캔이고.. 즉, 자동으로 빈등록하는거고

그냥 xml 로 빈등록하는 행위를 자바코드로도 할 수 있다.

그것이  Spring JavaConfig 프로젝트, @Configuration , @Bean 을 이용한 코드기반 설정이다.




착각하지 말아야 할것은 기본적으로.. 빈을 등록하는거랑 어노테이션은 ,  Configuration  어노테이션을 쓸때가 제외하면 별개의 내용이다.

즉 빈을 등록하는건 등록하는거고 (scan 등으로) 그걸 쓸때 어노테이션으로 자원을 가져와서 쓰는거다.

단  Configuration    는 빈을 등록하는것으로 이것을 제외하고 말이겠지?




@Repository ?? 
2.1에서 추가되었다나?  xml 에 선언하지 않고 빈을 등록하는 방법이 @Component만 있는게 아니다.
바로 @Repository 있다.

Component와 마찬가지로 스캔의 대상이 된다. 차이점은..

@Repository 는 DAO 기능을 수행하는 빈이 대상이라는데.. 뭐 -ㅅ-; 
그러면 @Component는? 서비스 레벨이라더라... 모호하면 Component과 DAO류면 @Repository ?? 

좀 그렇긴한데 우선 이렇게 이해해두자; 


 



+ Recent posts