Table of Contents
In this post , we will see how to configure spring with java based configuration. In previous post, we have already seen how to configure spring using xml configuration. Spring provides @configuaration and @Bean annotation for java based configuration.
Steps for spring java based configuration:
Project structure for below project:
1. Create a simple java maven project.
2. Maven dependency
put spring and cglib maven dependency in pom.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- cglib required for @configuration annotation --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.0</version> </dependency> </dependencies> <properties> <spring.version>4.2.1.RELEASE</spring.version> </properties> |
So your pom.xml will look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"< <modelVersion<4.0.0</modelVersion> <groupId>org.arpit.java2blog</groupId> <artifactId>SpringHelloWorldJavaBasedConfiguration</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringHelloWorldJavaBasedConfiguration</name> <description>It provides hello world program for java based config </description> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- cglib required for @configuration annotation --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.0</version> </dependency> </dependencies> <properties> <spring.version>4.2.1.RELEASE</spring.version> </properties> </project> |
3. Create Bean class
Create a bean class called country.java in package org.arpit.java2blog.model .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog.model; public class Country { String countryName; public Country(String countryName) { this.countryName=countryName; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } } |
4. Create application configuration class
This class will have @Configuaration and @Bean annotation .
Create class called ApplicationConfiguration.java in package org.arpit.java2blog.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog.config; import org.arpit.java2blog.model.Country; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ApplicationConfiguration { @Bean(name="countryObj") public Country getCountry() { return new Country("India"); } } |
Above file is equivalent to below spring configuration xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"< <context:annotation-config/< <bean id="countryObj" class="org.arpit.java2blog.Country" < <property name="countryName" value="India"/< </bean< </beans< |
5. Create main class to run the program
Create class called SpringJavaConfigMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog.main; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.arpit.java2blog.config.ApplicationConfiguration; import org.arpit.java2blog.model.Country;; public class SpringJavaConfigMain { public static void main(String[] args) { @SuppressWarnings("resource") ApplicationContext appContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class); Country countryObj = (Country) appContext.getBean("countryObj"); String countryName=countryObj.getCountryName(); System.out.println("Country name: "+ countryName); } } |
6. Run above program
When you run above program, you will get below output.
1 2 3 4 5 |
Feb 19, 2016 4:09:08 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2096ed8b: startup date [Fri Feb 19 16:09:08 IST 2016]; root of context hierarchy Country name: India |
7. Source code
Bingo!! we are done with spring java based configuaration.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.
Great Example to start