In this guide we discuss about Spring’s @Configuration annotation and how it works with examples.
1. @Configuration Annotation
In modern Spring applications, @Configuration is one of the most commonly used annotation. @Configuration annotation is used to tell that annotated class has one or more bean method definitions, so that spring can load and detect beans at runtime and make them available in Spring container.
Example:
@Configuration public class BirdConfig { @Bean public Eagle eagle() { return new Eagle(); } @Bean public Ostrich ostrich() { return new Ostrich(); } @Bean public Peacock peacock() { return new Peacock(); } }
@Component public class Eagle { @Override public String toString() { return "Eagle"; } }
2. Bootstrapping @Configuration
classes
@Configuration
classes are typically bootstrapped using AnnotationConfigApplicationContext
or in web mvc applications AnnotationConfigWebApplicationContext
.
A simple example:
public class BirdConfigurationTest { public static void main(String[] args) { ApplicationContext ctxt = new AnnotationConfigApplicationContext(BirdConfig.class); Eagle eagle = ctxt.getBean(Eagle.class); Ostrich ostrich = ctxt.getBean(Ostrich.class); Peacock peacock = ctxt.getBean(Peacock.class); System.out.println(eagle); System.out.println(ostrich); System.out.println(peacock); } }
Results:
15:41:28.084 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'birdConfig' 15:41:28.089 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'eagle' 15:41:28.101 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'ostrich' 15:41:28.102 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'peacock' Eagle Ostrich Peacock
3. Configuration classes in XML
Configuration
classes can be declared as normal <bean>
definitions within Spring Beans XML files. Which is useful if your bean definitions mixed in both XML files and Java Configuration classes or if you are migrating from Spring XML configurations to Java Configurations.
Example:
XML beans Configuration:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <bean class="com.javabydeveloper.spring.config.importannotation.BirdConfig"></bean> <bean class="com.javabydeveloper.spring.bean.XMLTestBean"> <property name="name" value="Student"></property> <property name="value" value="Peter"></property> </bean> </beans>
Testing:
public class BirdConfigurationXmlTest { public static void main(String[] args) { ApplicationContext ctxt = new ClassPathXmlApplicationContext("/configuration-beans-test.xml"); Eagle eagle = ctxt.getBean(Eagle.class); Ostrich ostrich = ctxt.getBean(Ostrich.class); Peacock peacock = ctxt.getBean(Peacock.class); XMLTestBean xmlTestBean = ctxt.getBean(XMLTestBean.class); System.out.println(eagle); System.out.println(ostrich); System.out.println(peacock); System.out.println(xmlTestBean); } }
Results:
16:47:51.258 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'com.javabydeveloper.spring.bean.XMLTestBean#0' 16:47:51.283 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'eagle' 16:47:51.294 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'ostrich' 16:47:51.294 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'peacock' Eagle Ostrich Peacock XMLTest [name=Student, value=Peter]
4. Conclusion
In this tutorial, we have learned how @Configuration annotation works in Spring Applications with examples.