[英]Mixing xml and java config with spring
我正在構建一個新的應用程序,通過java配置而不是xml配置spring。 此應用程序依賴於使用xml樣式配置的模塊。 當我嘗試啟動我的應用程序時,出現以下錯誤:
No qualifying bean of type [com.myModule.myServiceImp] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
該bean應該在模塊的applicationContext.xml中聲明。 處理這個問題的正確方法是什么? 我試着簡單地添加它,如果我在應用程序的web.xml中將應用程序上下文串起來的話:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:com/myModule/appbase-context.xml
com.myApp.AppConfig
</param-value>
</context-param>
但我仍然有同樣的錯誤。 這樣做的正確方法是什么?
在配置類中,您可以通過@ImportResource
批注導入xml配置。
像這樣的東西:
@Configuration
@ImportResource({"classpath:appbase-context.xml"})
public class AppConfig {
// @Bean definitions here...
}
請記住,當您使用Spring的Java配置時,您需要指定一個額外的context-param
,該context-param
表示要用於應用程序上下文的類:
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.