Import Spring XML files into @Configuration

This is common to mix XML configuration into Spring @Configuration, because developers are used to the XML namespaces. In Spring, you can use @ImportResource to import Spring XML configuration files into @Configuration :

AppConfig.java

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource("classpath:/config/spring.xml")
public class AppConfig {

}

Another example

AppConfig.java

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Import;

@Configuration
@Import({ AppConfigWeb.class })
@ImportResource("classpath:/config/spring.xml")
public class AppConfig {

}

P.S @ImportResource has been available since Spring 3.0

7 comments on “Import Spring XML files into @Configuration

  1. We have a huge spring xml-based project. We would like to change it to spring boot and it is not easy to change everything to @Beans. Just to be begin with, can I keep our old web.xml as it is if I set packaging to war in pom?
    Or web.xml should be converted to Config before we can run spring-boot?

    Reply
  2. FileNotFoundException: Could not open ServletContext resource [classpath:/main/webapp/WEB-INF/spring-servlet.xml]

    Tried many path combinations for xml file but didnt work.
    Please help.

    Reply
  3. is there a way to accomlish the same class with Annotations like @Configuration
    @Import({ AppConfigWeb.class })
    @ImportResource(“classpath:/config/spring.xml”)

    using spring2.5.6 ? I know this is available only from spring 3. Please let me know an alternative approach.

    Reply
  4. hi, I cannot find the Appconfig.java file? where should I create it or find it?

    Reply
  5. is there a way to import config in the xml file? i have a dependency project that is spring 4 but is being imported into a Spring 3 project which has XML configuration. The problem is i cannot use autowired beans as the parent project is not aware of those beans for autowiring

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *