spring之在classpath中扫描组件(3)
spring之在classpath中扫描组件(3)
----------
?
过滤扫描的组件
?
默认情况下,Spring将侦测所有使用了特定注解的类,这些注解包括:@Component,@Repository,@Service,@Controller,以及被@Component所注解的定制注解类型。这里,还可以通过应用一个或多个include/exclude过滤器来定制扫描。
Spring支持4种类型的过滤器表达式。其中,annotation和assignable用于指定需要过滤的注解类型和类/接口。regex和aspectj类型则允许指定正则表达式和AspectJ切入点表达式,用这些表达式来进行类的匹配。例如,下面的组件扫描就包括了所有名称包含Dao和Service单词的类,同时,对于具有@Controller注解的类,则不进行扫描。
<beans ...><context:component-scan base-package="com.apress.springrecipes.sequence"><context:include-filter type="regex"expression="com\.apress\.springrecipes\.sequence\..*Dao.*"/><context:include-filter type="regex"expression="com\.apress\.springrecipes\.sequence\..*Service.*"/><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller"/></beans>
因为这里使用了include过滤器,这些过滤器将侦测所有名称包含Dao和Service单词的类,所以即使没有使用标识组件的注解,SequenceDaoImpl和SequenceService组件也会被自动侦测到。
?
?
?
?