The Spring 4 xml configuration example shows you how to use Spring XML Based Configuration via a simple Spring application. This application involves a service which will display a simple message to the console and we configure this application with Spring XML. Then, we bootstrap the Spring context(ApplicationContext) using the ClassPathXmlApplicationContext.
Maven Dependencies
To build this application, we need to add the spring-core and spring-context dependencies to the classpath.
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 |
<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>com.javabycode.spring</groupId> <artifactId>xml-based-configuration</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>SPRING-CORE - ${project.artifactId}</name> <url>http://javabycode.com</url> <properties> <spring.version>4.3.0.RELEASE</spring.version> </properties> <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> </dependencies> </project> |
Create Spring Service
We create the HelloService class is used to demonstrate how to use spring XML based configuration.
1 2 3 4 5 6 7 8 |
package com.javabycode.spring; public class HelloService { public void sayHello(String msg) { System.out.println("Hello " + msg); } } |
Spring XML Based Configuration
A spring configuration is a xml file which consists of at least one or more than one bean definition. These beans are registered with Spring container using the
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <description> Configure spring application using XML based configuration </description> <bean class="com.javabycode.spring.HelloService"/> </beans> |
Demo Spring ApplicationContext
We start the ApplicationContext using the ClassPathXmlApplicationContext. We pass the location of spring-config.xml file(Spring XML Based Configuration) using an array of strings. Then, we use the getBean method with the paramatter class name to get the spring managed bean. Finally we can call the makeHello method of the HelloService.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javabycode.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyApplication { public static void main(String... args){ ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"}); HelloService coffeeMachine = context.getBean(HelloService.class); coffeeMachine.sayHello("Spring 4 xml configuration example"); } } |
Run the main class above, the output will be printed like below
1 |
Hello Spring 4 xml configuration example |
References
ClassPathXmlApplicationContext JavaDoc
Download complete source code, please click link below
spring-4-xml-configuration-example.zip (262 downloads)