将 xml 和 java 配置与 spring 混合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19365366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mixing xml and java config with spring
提问by jensengar
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:
我正在构建一个新的应用程序,它通过 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)}
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:
这个 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>
But I still got the same error. What is the proper way to do this?
但我仍然遇到同样的错误。这样做的正确方法是什么?
采纳答案by nicholas.hauschild
In your configuration class, you can import xml configuration via the @ImportResource
annotation.
在你的配置类中,你可以通过@ImportResource
注解导入xml配置。
Something like this:
像这样的东西:
@Configuration
@ImportResource({"classpath:appbase-context.xml"})
public class AppConfig {
// @Bean definitions here...
}
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:
请记住,当您使用 Spring 的 Java 配置时,您需要指定一个附加项context-param
,说明要用于您的应用程序上下文的类:
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>