jensengar
jensengar

Reputation: 6167

Mixing xml and java config with spring

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)}

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?

Upvotes: 25

Views: 27030

Answers (1)

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

In your configuration class, you can import xml configuration via the @ImportResource annotation.

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:

<context-param>
    <param-name>contextClass</param-name>
    <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
</context-param>

Upvotes: 48

Related Questions