java 在 <context:component-scan> 中包含子包的语法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6807230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What syntax for including sub-packages in <context:component-scan>?
提问by Horace
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" />
回答by skaffman
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?
这可以轻松快捷地自行验证 - 您是否尝试过?
回答by danny.lesnik
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
或本身用 @Component 注释的自定义注释是唯一检测到的候选组件。
You can change this behavior by applying custom filters which are include-filter
or exclude-filter
您可以通过应用自定义过滤器来更改此行为,这些过滤器是 include-filter
或exclude-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 注释。