Table of Contents
In this post, we will see how to create Spring hello world XML based configuration example.
Here are simple steps to create Spring XML configuration example.
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 20 21 22 23 24 25 26 27 |
package org.arpit.java2blog.model; public class Country { private String name; private long population; public Country(String name, long population) { super(); this.name = name; this.population = population; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } } |
4. ApplicationContext.xml
Create ApplicationContext.xml in src/main/resources as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <?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" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <bean id="country" class="org.arpit.java2blog.model.Country"> <constructor-arg index="0" value="India"></constructor-arg> <constructor-arg index="1" value="20000"></constructor-arg> </bean> <context:annotation-config /> </beans> |
Please note that we are using Constructor injection to inject dependencies here.
5. Create SpringXMLConfigurationMain.java
Create a class named "SpringXMLConfigurationMain.java" in org.arpit.java2blog package.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog; import org.arpit.java2blog.model.Country; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Spring XML based configuration example * */ public class SpringXMLConfigurationMain { public static void main( String[] args ) { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Country countryBean = (Country) ac.getBean("country"); String name = countryBean.getName(); System.out.println("Country name: "+name); long population = countryBean.getPopulation(); System.out.println("Country population: "+population); ac.close(); } } |
6. Run it
When you run above program, you will get below output.
Jun 15, 2018 11:21:58 AM Jun 15, 2018 9:04:19 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@210366b4: startup date [Fri Jun 15 21:04:19 IST 2018]; root of context hierarchy
Jun 15, 2018 9:04:20 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [ApplicationContext.xml]
Country name: India
Country population: 20000
Jun 15, 2018 9:04:20 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@210366b4: startup date [Fri Jun 15 21:04:19 IST 2018]; root of context hierarchy
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@210366b4: startup date [Fri Jun 15 21:04:19 IST 2018]; root of context hierarchy
Jun 15, 2018 9:04:20 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [ApplicationContext.xml]
Country name: India
Country population: 20000
Jun 15, 2018 9:04:20 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@210366b4: startup date [Fri Jun 15 21:04:19 IST 2018]; root of context hierarchy
That’s all about Spring XML configuration.
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.