웹 프로그래밍/[ Spring ]
[ Spring ] 08. Component Scan
kim.svadoz
2021. 6. 30. 16:48
반응형
Component Scan
xml 설정파일에 서 모든 빈을
으로 일일이 등록해줘야 하는 것은 매우 번거로운 일이다 그래서 등장한것이 Component Scan이다.
test.xml
<context:component-scan base-package="com.multi.app" />
위와 같이 내부 filter 태그가 없다면, base-package에 지정한 패키지에서부터 모든 하위 패키지를 scanning해서 빈을 등록하도록 한다.
모든 클래스를 빈으로 등록하는 것은 아니고,
@Component
Annotation이 붙은 클래스를 빈으로 등록한다.ApplicationContext.xml
<context:component-scan base-package="com.myapp.core, com.myapp.app"> <!-- Component-scan대상에서 @Controller annotation Class는 제외한다. --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
Servlet-Context.xml
<context:component-scan base-package="com.myapp.app" use-default-filters="false"> <!-- Component-scan대상은 @Controller annotation Class만 scan한다. --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
위를 보면 Servlet-Context.xml 설정 시,
use-default-filters
속성을false
로 처리 하였다.
use-default-filters
속성은 원래default
가true
인데, @Component Annotation(@Controller, @Service, @Repository
등..)의 클래스를 자동으로 Bean으로 등록해주는 filter 속성이다.따라서 위의 filter를 false로 변경하고 scan할 대상에 대한 Annotation만 include-filter에 추가하였다.
반응형