简体   繁体   English

在<context:component-scan>中包含子包的语法是什么?

[英]What syntax for including sub-packages in <context:component-scan>?

I am using Spring and I have a long list of subpackages, do I have to specify them one by one in the <context:component-scan> tag? 我使用的是Spring,我有很长的子包列表,我是否必须在<context:component-scan>标记中逐一指定它们?

<context:component-scan base-package="com.fooapp.mainpackage, 
com.fooapp.mainpackage.subpackage1, 
com.fooapp.mainpackage.subpackage2, 
com.fooapp.mainpackage.subpackage3" />

Component-scanning supports package hierarchies, so this should work: 组件扫描支持包层次结构,因此这应该工作:

<context:component-scan base-package="com.fooapp.mainpackage"/>

This is easy and quicker to verify for yourself - did you try it? 这样可以轻松快速地自行验证 - 您是否尝试过?

In addition I would to add that by default, classes annotated with @Component , @Repository , @Service , @Controller , or a custom annotation that itself is annotated with @Component are the only detected candidate components. 另外,我想补充一点,默认情况下,使用@Component@Repository@Service @Controller@Repository注释的@Component或者自己用@Component注释的自定义注释是唯一检测到的候选组件。

You can change this behavior by applying custom filters which are include-filter or exclude-filter 您可以通过应用include-filterexclude-filter自定义过滤器来更改此行为

For example: 例如:

<context:component-scan base-package="com.vanilla">
      <context:exclude-filter 
                type="annotation"
                expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

It will exclude all @Repository annotations. 它将排除所有@Repository注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM