Combining Annotation and XML Configurations in your Spring 3 Applications

Spring framework

Combining Annotation and XML Configurations in your Spring 3 Applications

By Jason Todhunter

How to Combine Spring XML and Annotation Configuration Files

comment on this article

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.

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=""
  <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

comment on this article


01 Nov 2010

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.