반응형

@Componentscan 애노테이션은 @Component, @Controller, @Service, @Repository 애노테이션을 가지고 있는 클래스들을 자동으로 Scan하여 Bean으로 등록하는 역할을 하고 있습니다.

 

최근의 스프링에서는 Componentscan을 xml에서 설정하지 않고, Java 단에서 애노테이션을 설정을 하고 있습니다. 하지만, 아직까지 스프링 레거시 프로젝트를 사용하고 있는 프로젝트에서는 대부분 xml에서 설정하고 있고, 지금 진행하고 있는 프로젝트에서도 xml에서 Componentscan을 설정하고 있기 때문에 정리하려고 합니다.

 

context:component-scan

context:component-scan을 xml에서 처리하는 경우에는 <beans></beans> 태그 안에 설정을 해야 합니다. 즉, 아래의 코드처럼 선언을 할 수 있습니다.

<beans>
	<context:component-scan base-package="sample">
		
	</context:component-scan>
</beans>

 

base-package

위의 코드에서 context:component-scan의 예시 코드를 보면 base-package를 볼 수 있습니다. 이 base-package는 Componentscan이 자동으로 Scan을 할 때, 어디서부터 시작할건지 시작점을 지정해주는 부분입니다. 지금, 위의 코드에서 base-package는 sample로 되어 있으니, sample부터 시작하여 자동으로 Scan을 합니다. 

 

여기서, base-package는 1개가 아닌 2개 이상을 지정할 수 있습니다. 아래의 코드처럼 말이죠.

<beans>
	<context:component-scan base-package="sample, sample2">
		
	</context:component-scan>
</beans>

 

이제, 아래의 코드처럼 xml에 component-scan을 설정하면 default로 @Component, @Controller, @Service, @Repository 애노테이션이 선언된 클래스들은 default로 Scan을 합니다.

<beans>
	<context:component-scan base-package="sample">
		
	</context:component-scan>
</beans>
package sample;

@Controller
public  class  SampleController{
	
}

 

component-scan은 default로 @Component, @Controller, @Service, @Repository 애노테이션이 선언된 클래스를 default로 Scan을 하지만, 이 @Component, @Controller, @Service, @Repository 애노테이션을 default로 스캔하지 않는 방법도 있습니다. 바로, use-default-filters를 false로 선언하면 됩니다. use-default-filters은 default가 true로 선언이 되어 있습니다.

<beans>
	<context:component-scan base-package="sample" use-default-filters="false">
		
	</context:component-scan>
</beans>

 

또한, default로 @Component, @Controller, @Service, @Repository 애노테이션을 스캔하지 않는 방법 외에도 @Component나 @Controller, @service 등을 1개만 스캔할 수 있는 방법도 있습니다. 이 경우에는 include-filter와 exclude-filter를 사용하면 됩니다.

 

context:include-filter

@Controller Scan 제외

<beans>
	<context:component-scan base-package="sample">
    	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
</beans>

 

context:exclude-filter

@Service Scan 제외

<beans>
	<context:component-scan base-package="sample">
    	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
	</context:component-scan>
</beans>

 

context:include-filter / context:exclude-filter 혼용

@Controller만 Scan하고, @Repository, @Service는 Scan 제외

<beans>
	<context:component-scan base-package="sample">
    	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
	</context:component-scan>
</beans>

 

지금 프로젝트에서도 <context:component-scan>은 2군데에서 스캔을 하고 있다. 이 2군데를 A와 B로 나타내면, A는 지금 default를 모두 Scan하고 있고, B에서는 @Repository와 @Service를 스캔하지 않고, @Controller만 스캔을 한다. 이처럼, 이렇게 나누는 것보다는 @ComponentScan 애노테이션을 Java단에서 사용할 경우에는 좀 더 직관적으로 볼 수 있어 지금은 xml에서 Java단으로 이동하고 있는 추세다.

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기