問題描述
我正在使用 Spring 3.1 并使用 @Configuration
和 @ComponentScan
屬性引導應用程序.
I'm using Spring 3.1 and bootstrapping an application using the @Configuration
and @ComponentScan
attributes.
真正的開始是用
new AnnotationConfigApplicationContext(MyRootConfigurationClass.class);
這個配置類被注釋了
@Configuration
@ComponentScan("com.my.package")
public class MyRootConfigurationClass
這很好用.但是我想更具體地了解我掃描的包,所以我嘗試了.
and this works fine. However I'd like to be more specific about the packages I scan so I tried.
@Configuration
@ComponentScan("com.my.package.first,com.my.package.second")
public class MyRootConfigurationClass
但是這失敗了,錯誤提示我找不到使用 @Component
注釋指定的組件.
However this fails with errors telling me it can't find components specified using the @Component
annotation.
我所追求的正確方法是什么?
What is the correct way to do what I'm after?
謝謝
推薦答案
@ComponentScan 使用字符串數組,像這樣:
@ComponentScan uses string array, like this:
@ComponentScan({"com.my.package.first","com.my.package.second"})
當你只在一個字符串中提供多個包名時,Spring會將其解釋為一個包名,因此無法找到它.
When you provide multiple package names in only one string, Spring interprets this as one package name, and thus can't find it.
這篇關于如何使用@ComponentScan 注解掃描多個路徑?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!