Spring framework

Combining Annotation and XML Configurations in your Spring 3 Applications

By Jason Todhunter

TheServerSide.com

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


 

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.