我有一個名爲com.example.service
的包,而在我的Spring配置類中,我有註釋@ComponentScan({"com.example.service"},{"com.example.controller"})
。Spring @ComponentScan for @Service
當我嘗試使用@Autowire
服務時,代碼編譯失敗,出現NoSuchBeanDefinitionException
。我的服務界面注有@Service
。
目前我使用一個相當難看的解決辦法,在我的ExampleConfig.java像
@Bean
public MyService myService() {
return new MyServiceImpl();
}
申報的每一個服務豆一般的@ComponentScan
似乎工作,如果我刪除控制器封裝,都沒有找到控制器。我理解錯了什麼?請讓我知道,如果我錯過了任何相關信息。
編譯時不會發生異常。保持錯誤類型分開,否則你會在錯誤的地方尋找解決方案。 – chrylis 2014-11-04 08:58:02
你對此絕對正確。在這一點上,我的描述與我的描述不精確。 – dexBerlin 2014-11-04 09:29:08