In this Spring XML configuration example we’ll see how to create a Spring application where bean declaration and bean dependencies configuration is done using XML configuration.
For this Spring XML configuration example Maven build tool is used for managing dependencies. Please refer Create Java Project Using Maven in Eclipse to see how to create a Maven project.
Maven dependencies
For this example we need spring core and spring context dependencies. Spring Version used is 5.1.8 Release which is defined under properties element in pom.xml.
- Spring core provides the basic framework classes and classes to interact with other modules.
- Spring context module provides
org.springframework.context.ApplicationContext
interface which represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.
<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.knpcode</groupId> <artifactId>SpringProject</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringProject</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>5.1.8.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <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>
Bean classes
In the example there is a class to place orders called Order and purchase can be done from a store. Order and Store beans and bean dependencies will be configured using Spring XML configuration.
public interface IStore { public void doPurchase(int items); }
public class Store implements IStore { public void doPurchase(int items) { System.out.println("Doing purchase of " + items +" Items"); } }
public class Order { private IStore store; private int items; public Order(IStore store, int items) { this.store = store; this.items = items; } public void setStore(IStore store) { this.store = store; } public void buyItems() { store.doPurchase(items); } }
As you can see from the classes Order has a dependency on Store (store field in Order class which is of type IStore).
Spring XML Configuration
Create an XML file appContext.xml in src/main/resources package. This XML file is used for creating ApplicationContext.
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Store bean --> <bean id="store" class="com.knpcode.SpringProject.Store" /> <!-- Order bean with dependencies --> <bean id="order" class="com.knpcode.SpringProject.Order"> <constructor-arg ref="store" /> <constructor-arg type="int" value="20" /> </bean> </beans>Note that here dependencies are injected as constructor dependency injection.
You can use the following class with main method to read the configuration and call the bean method. There are several implementations of the ApplicationContext interface in Spring framework. In standalone applications, it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext.
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ){ // create context using configuration ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml"); Order order = context.getBean("order", Order.class); order.buyItems(); // close the context context.close(); } }Output
17:48:34.049 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [appcontext.xml] 17:48:34.173 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'store' 17:48:34.217 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'order' Doing purchase of 20 Items
That's all for the topic Spring XML Configuration Example. If something is missing or you have something to share about the topic please write a comment.
You may also like
No comments:
Post a Comment