In this Spring XML Configuration Example, we will be creating a simple spring application using the spring xml configurations which displays Book and Library details and we will also be injecting book reference into library class.
Folder Structure:
- Create a simple Maven webapp Project “SpringCoreExample” and create a package for our source files “com.javainterviewpoint” under src/main/java
- Now add the following dependency in the POM.xml
<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.javainterviewpoint</groupId> <artifactId>SpringCoreExample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringCoreExample</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>5.1.1.RELEASE</spring.version> </properties> <dependencies> <!-- Spring Dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project>
- Create the Java class Application.java, Book.java and Library.java under com.javainterviewpoint folder.
- Place the SpringConfig.xml under src/main/java folder
Spring XML Configuration Example
Dependency Tree
[INFO] ------------------------------------------------------------------------ [INFO] Building SpringCoreExample 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ SpringCoreConfiguration --- [INFO] com.javainterviewpoint:SpringCoreExample:jar:0.0.1-SNAPSHOT [INFO] +- org.springframework:spring-core:jar:5.1.1.RELEASE:compile [INFO] | \- org.springframework:spring-jcl:jar:5.1.1.RELEASE:compile [INFO] +- org.springframework:spring-beans:jar:5.1.1.RELEASE:compile [INFO] \- org.springframework:spring-context:jar:5.1.1.RELEASE:compile [INFO] +- org.springframework:spring-aop:jar:5.1.1.RELEASE:compile [INFO] \- org.springframework:spring-expression:jar:5.1.1.RELEASE:compile [INFO] ------------------------------------------------------------------------
Book.java
Book class is a simple class consisting of the book details such title, author, publications and its corresponding POJO’s.The getBookDetails()method will display the book information which is set.
package com.javainterviewpoint; public class Book { private String title; private String publications; private String author; public Book() { super(); } public Book(String title, String publications, String author) { super(); this.title = title; this.publications = publications; this.author = author; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPublications() { return publications; } public void setPublications(String publications) { this.publications = publications; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public void getBookDetails() { System.out.println("**Published Book Details**"); System.out.println("Book Title : " + title); System.out.println("Book Author : " + author); System.out.println("Book Publications : " + publications); } }
Library.java
Library class has the Book class instance as a property and its corresponding getters and setters. The book property will get its value through our configuration file.
package com.javainterviewpoint; public class Library { private Book book; public void setBook(Book book) { this.book = book; } public Book getBook() { return book; } }
SpringConfig.xml
In our configuration file we have defined seperate id for each bean Library and Book classes
<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-3.0.xsd"> <bean id="library" class="com.javainterviewpoint.Library"> <property name="book" ref="book"></property> </bean> <bean id="book" class="com.javainterviewpoint.Book"> <property name="title" value="Spring XML Configuration"></property> <property name="author" value="JavaInterviewPoint"></property> <property name="publications" value="JIP Publication"></property> </bean> </beans>
- We inject primitives to the Book class properties such as title, author, publications.
<bean id="book" class="com.javainterviewpoint.Book"> <property name="title" value="Spring XML Configuration"></property> <property name="author" value="JavaInterviewPoint"></property> <property name="publications" value="JIP"></property> </bean>
- We are referencing the Book class bean id to the property book of the Library class
<property name="book" ref="book"></property>
- The ref passed to the property book should be the bean id of the Book Class. In short
ref =<<bean id of Book class>>
Application.java
package com.javainterviewpoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String args[]) { // Read the Spring configuration file [SpringConfig.xml] ApplicationContext appContext = new ClassPathXmlApplicationContext("SpringConfig.xml"); // Get the Library instance Library library = (Library) appContext.getBean("library"); // Get the Book Details library.getBook().getBookDetails(); } }
- ApplicationContext class reads our Configuration File(SpringConfig.xml) and obtain all the bean definition mentioned in the config file.
- The Library class instance is obtained by calling the getBean() method over the ApplicationContext.
- The String passed to getBean() method should be equivalent to the id defined in the SpringConfig.xml
- Since we have already injected Book object to the property book in Library class, we can display the book details by simply calling the getBookDetails() method on top of library.getBook()
Output
Dec 05, 2018 6:20:13 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5cb0d902: startup date [Wed Dec 05 18:20:13 IST 2018]; root of context hierarchy Dec 05, 2018 6:20:13 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [SpringConfig.xml] **Published Book Details** Book Title : Spring XML Configuration Book Author : JavaInterviewPoint Book Publications : JIP Publication
Leave a Reply