Spring @Configuration annotation helps in Spring annotation based configuration. @Configuration annotation indicates that a class declares one or more @Bean
methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
Since spring 2, we were writing our bean configurations to xml files. But Spring 3 gave the freedom to move bean definitions out of xml files. we can give bean definitions in Java files itself. This is called Spring Java Config feature (using @Configuration
annotation).
1. Spring @Configuration annotation usage
Use @Configuration
annotation on top of any class to declare that this class provides one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
@Configuration public class AppConfig { @Bean(name="demoService") public DemoClass service() { } }
2. Spring @Configuration annotation example
To understand @Configuration
annotation usage, let’s see it in action.
2.1. Create maven project
mvn archetype:generate -DgroupId=com.howtodoinjava.core -DartifactId=springCoreTest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false mvn eclipse:eclipse
2.2. Update Spring dependencies
Update maven dependencies. I have added Spring 5 deendencies.
<properties> <spring .version>5.0.6.RELEASE</spring> </properties> <!-- Spring 5 dependencies --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>${spring.version}</version> </dependency> <!-- For JavaConfig --> <dependency> <groupid>cglib</groupid> <artifactid>cglib</artifactid> <version>2.2.2</version> </dependency>
2.3. Create spring beans
public interface DemoManager { public String getServiceName(); } public class DemoManagerImpl implements DemoManager { @Override public String getServiceName() { return "My first service with Spring 3"; } }
2.4. Spring configuration class with @Configuration annotation
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.howtodoinjava.core.beans.DemoManager; import com.howtodoinjava.core.beans.DemoManagerImpl; @Configuration public class ApplicationConfiguration { @Bean(name="demoService") public DemoManager helloWorld() { return new DemoManagerImpl(); } }
3. Demo
Lets write the test code and run. This should be able to configure bean and we should be able to use it.
package com.howtodoinjava.core.verify; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.howtodoinjava.core.beans.DemoManager; import com.howtodoinjava.core.config.ApplicationConfiguration; public class VerifySpringCoreFeature { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class); DemoManager obj = (DemoManager) context.getBean("demoService"); System.out.println( obj.getServiceName() ); } }
Happy Leaning !!
Leave a Reply