将XML和Java配置与Spring混合 [英] Mixing xml and java config with spring
问题描述
我正在构建一个新的应用程序,该应用程序通过java config(而不是xml)配置spring.此应用程序依赖于使用xml样式配置的模块.当我尝试启动我的应用程序时,出现以下错误:
I am building a new application that configures spring through a java config rather than xml. This app is dependent on a module that uses the xml style config. When I try and launch my app, I get the following error:
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中将应用程序上下文串联在一起那样简单地添加它:
This bean should be declared in the module's applicationContext.xml. What is the proper way to handle this? I tried simply adding it as I would if I was stringing application contexts together in the app's web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:com/myModule/appbase-context.xml
com.myApp.AppConfig
</param-value>
</context-param>
但是我仍然遇到同样的错误.正确的方法是什么?
But I still got the same error. What is the proper way to do this?
推荐答案
In your configuration class, you can import xml configuration via the @ImportResource
annotation.
类似这样的东西:
@Configuration
@ImportResource({"classpath:appbase-context.xml"})
public class AppConfig {
// @Bean definitions here...
}
请记住,在使用Spring的Java配置时,您需要指定一个附加的context-param
,该context-param
表示要用于您的应用程序上下文的类:
Remember, when you are using Spring's Java Configuration, you need to specify an additional context-param
that says the class to use for your application context:
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
这篇关于将XML和Java配置与Spring混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!