Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing xml and java config with spring

Tags:

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?

like image 432
jensengar Avatar asked Oct 14 '13 17:10

jensengar


People also ask

Can we use both annotation and XML based configuration?

Yes, you can use XML, annotations, or a mix.

Does Spring boot support XML configuration?

Spring allows you to configure your beans using Java and XML.

Does Spring 5 support XML configuration?

XML configuration is still officially supported by Spring.


1 Answers

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> 
like image 163
nicholas.hauschild Avatar answered Nov 03 '22 00:11

nicholas.hauschild