用spring混合xml和java配置

jen*_*gar 25 java xml 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)}
Run Code Online (Sandbox Code Playgroud)

该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>
Run Code Online (Sandbox Code Playgroud)

但我仍然有同样的错误.这样做的正确方法是什么?

nic*_*ild 46

在配置类中,您可以通过@ImportResource注释导入xml配置.

像这样的东西:

@Configuration
@ImportResource({"classpath:appbase-context.xml"})
public class AppConfig {
    // @Bean definitions here...
}
Run Code Online (Sandbox Code Playgroud)

请记住,当您使用Spring的Java配置时,您需要指定一个附加内容context-param,说明要用于应用程序上下文的类:

<context-param>
    <param-name>contextClass</param-name>
    <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)