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.

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:

@Configuration
@ImportResource("classpath:meanbeans.xml")
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");

    System.out.println(beanFactory.getBean("fooBean"));
    System.out.println(beanFactory.getBean("barBean"));
  }
}

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:

@Configuration
@ImportResource("classpath:meanbeans.xml")
class JConfig {
  @Bean()
  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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
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"
default-autowire="autodetect"
>
  <bean id="barBean" class="com.mcnz.spring.BarBean" />
</beans>

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


 

This was last published in November 2010

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudApplications

SearchSOA

SearchSoftwareQuality

SearchFinancialApplications

SearchSAP

SearchManufacturingERP

Close