我正在使用Spring,有很多子包,我是否需要逐个在<context:component-scan>
标签中指定它们?
<context:component-scan base-package="com.fooapp.mainpackage,
com.fooapp.mainpackage.subpackage1,
com.fooapp.mainpackage.subpackage2,
com.fooapp.mainpackage.subpackage3" />
组件扫描支持包层次结构,因此这应该可行:
<context:component-scan base-package="com.fooapp.mainpackage"/>
这很容易且更快速,你可以自己验证一下 - 你试过了吗?
@Component
、@Repository
、@Service
、@Controller
或被注解为@Component
的自定义注解的类会被检测作为组件候选者。include-filter
或exclude-filter
)来更改此行为。<context:component-scan base-package="com.vanilla">
<context:exclude-filter
type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<context:component-scan base-package="com.fooapp.mainpackage.service"/>
等其他层,这只是一个想法。 - Rachel