将XML和Java配置与Spring混合 [英] Mixing xml and java config with spring

查看:102
本文介绍了将XML和Java配置与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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆