我有两个Spring Boot
项目让我们调用它们Foo
和Bar
. Bar
包含在Foo
作为依赖项中。现在我只想包含来自的特定包Bar
(Component Scan
比方Foo
说:)com.example.bar.ctrl
。我知道您可以通过排除软件包,excludeFilters
但这会花费更多的精力,并且exclude filter-list
每次在Bar
. 存档该行为的最佳方法是什么?
问问题
2781 次
2 回答
4
我会这样做:
1) Foo 包将包含所有 bean com.example.foo
,@SpringBootApplication
主类也将在该包中,因此它将扫描com.example.foo
包中的所有 bean。
2)在 bar packagecom.example.bar.ctrl
中,我会创建没有任何属性@Configuration
的@ComponentScan
注释,所以它只会扫描ctrl
包+子包。
3)我@Import
将从点2)配置类到从点1)的主类)
@ComponentScan
'sinclude
或exclude
属性不需要显式包。
于 2016-07-20T09:21:45.453 回答
0
如果您正在使用@ComponentScan
注释,那么它有一个basePackages
属性,它需要一个String[]
包来扫描。
于 2016-07-20T09:20:10.300 回答