Developing in Spring with XML Configuration

Let us look at developing a simple Spring Application with XML configuration to understand how Spring works.
For the purpose of this exercise, we will use the IntelliJ Idea IDE.

Creating a Spring Application

In your IDE, go to File -> New Project.
Select Maven -> Create From Archetype -> Quickstart


Click Next and name the project.
Click Next -> Finish

(Follow the previous blog on Creating a Simple Maven Project for in depth information on creating a Maven project)

Navigate to the pom.xml file and add the dependency for Spring. 

Paste the following code in order to add Spring dependencies to your project.

  <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
(The version may change over time)

Inside the main package, in addition to the current App class, create another class has been created called Box.

This class has one void method to depict an action that a box can do.


If you wish to call this method, the following steps can be followed;


Running this class will give the following output;


Further, another class like box can be created to perform the same functions.



And gives the following output;



This method, however, implies tight coupling. We want the modules to not depend on each other so closely, therefore we use Spring.

Let us look at the dependency within the given code;


In order to reduce the number of dependencies, we can add an interface to aid with abstraction. 


Following the declaration of the Interface, the following changes must be made to the Box and Crate classes.



Thereafter, the main method can be changed as follows;

 

(For this example, we are looking at the Box object)

This shows only one dependency.

Even this method, however is tightly coupled, as creation of a new object with the new keyword can be considered tight coupling. In order to change this, we can invoke the getBean() method as follows;


If you try to run this file as is, it will compile with errors.

Let us now see how to configure the xml file in order to make sense of the example.xml and container.


This is the code in the example.xml file.

In the <bean> tag, the given id is the parameter given to the getBean() method shown above, whereas the class is the class which is being referred by the xml file.

The definition provided in the <beans> tag must be given as shown;
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">
Now, when you run the class, it will give you the following output:


If you want to change the configuration so that the object that is run is from the Box class, but from the Crate class, you simply need to make the changes in the xml file as follows;


Running the App.java class now, will give the following output;


To accomplish this, we made no changes to the App,java class, but simply changed the XML configuration.

This highlights what is meant by dependency injection and how you can attain loose coupling through the Spring framework.

Isn't that absolutely cool?

This is possible due to the dependency injection of Spring.

Make sure that when using the IntelliJ Idea IDE, you create a new Resources directory in order to store the XML configuration file. If not, it will give errors.


In future, we will look into other configurations of Spring as well.

Happy Springing!




Comments

Popular Posts