Combining Annotation and XML Configurations in your Spring 3 Applications

Who says you need to choose between XML and annotation based configurations. Why not use both! It's as easy as a little import annotation at the top of your config file.

Spring 3 provides the ability to use a Java based configuration file, as opposed to using those monolithic XML configuration files that everyone hates. However, it doesn't have to be a complete "annotations vs. xml" debate, as you can easily combine both approaches.

We've updated our Spring, Spring Boot and Spring MVC tutorials. You might want to check these newer links out...

TheServerSide has been updating its resources in terms of the Spring framework, Java EE contexts and various different approaches to inversion of control (IoC) and dependency injection (DI). Check out these newer resources on the topic:

If you are using a Java @Configuration file, you can simultaneously load in an XML configuration file simply by adding an @ImportResource annotation. The following annotation loads in a Spring XML configuration file named meanbeans.xml into the annotation based Spring ApplicationContext object:

class JConfig { }

So, if you've got a class named FooBean and a class named BarBean, and you have one bean configured in the XML file, and the other configured in the Java @Configuration file, you can pull them both into your application from a single beanFactory.

class FooBean { }
class BarBean { }

So, this would be the goal:

public class XmlAndAnnotations {

  public static void main(String args[]) {
    AnnotationConfigApplicationContext beanFactory =
    new AnnotationConfigApplicationContext(JConfig.class);
      //new ClassPathXmlApplicationContext("meanbeans.xml");


Notice how both the fooBean and barBean are pulled from a common beanFactory, despite the two beans being configured in different configuration resources? Here's the full Java based configuration file, which only specifically describes the creation of the fooBean:

class JConfig {
  public FooBean fooBean() {return new FooBean();}

And here's the meanbeans.xml file, which describes the barBean:

<beans xmlns="http://www.springframework.org/schema/beans"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
  <bean id="barBean" class="com.mcnz.spring.BarBean" />

And that's it! When the main method of the XmlAndAnnotations class runs, both the fooBean and barBean will be loaded from two separate Spring configuration resources!

comment on this article

Recommended Books for Learning Spring

Spring in Action  ~ Craig Walls
Spring Recipes: A Problem-Solution Approach  ~ Gary Mak
Professional Java Development with the Spring Framework ~ Rod Johnson PhD
Pro Java EE Spring Patterns: Best Practices and Design Strategies ~ Dhrubojyoti Kayal

Spring 3 Annotation File with @ImportResources and @Configuration

Spring 3 Annotations vs XML Combined Configuration


Next Steps

New to Git and distributed version control? Here are some Git examples and Jenkins-Git integration tutorials designed to help you master the popular source code versioning tool.

Dig Deeper on Front-end, back-end and middle-tier frameworks

App Architecture
Software Quality
Cloud Computing