[SPRING] context:component-scan

Yuri Lee·2022년 12월 15일
0

스프링

목록 보기
12/13

context:component-scan 사용법

@Controller, @Service, @Repository, @Component 등의 애노테이션을 component-scan을 xml에 선언해서 사용하고 있다.

<context:component:scan base-package="com.zorba.chocolate, com.zorba.redWine">

base-package는 패키지를 어디부터 스캔할지 지정해주는 부분이다. 스캔할 패키지를 여러개로 지정해줄 수도 있다.

디폴트(@Component, @Controller, @Service, @Repository) 애노테이션을 스캔하지 않는 방법도 있는데, use-default-filters를 이용하면 된다. 기본값은 true이기 때문에 false로 선언해주면 디폴트 애노테이션들을 스캔하지 않는다.

<context:component-scan base-package="com.zorba.redWine">
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<context:component-scan base-package="com.zorba.redWine" use-default-filters="false">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

include-filter 옵션을 사용하면 해당되는 애노테이션을 스캔대상에 포함할 수 있고, exclude-filter옵션을 사용하면 해당되는 애노테이션을 스캔대상에서 제외할 수 있다.

include-filter와 use-default-filters 옵션들을 사용해 디폴트 애노테이션들을 스캔대상에서 제외하고 @Controller만 스캔할 수 있도록 설정하였다.

references

https://zorba91.tistory.com/249

profile
개발자 이유리

0개의 댓글