Spring XML configuration example

Previous
Next

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.

So your pom.xml will look like:

3. Create Bean class

Create a bean class called Country.java in package org.arpit.java2blog.model .

4. ApplicationContext.xml

Create ApplicationContext.xml in src/main/resources as below.

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.

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

That’s all about Spring XML configuration.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog