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:
- Inversion of control explained fast with a Spring IoC example
- Spring MVC Java web development with Spring Boot
- Drawbacks to IoC problems and how to solve inversion of control problems
- Spring IoC vs. Google Guice's inversion of control approach
- Getting started with Google Guice
- Getting started with the Spring IoC container
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="https://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!
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