Vindya Gunawardana
5 min readOct 22, 2017

--

Spring XML Configuration using Setter Injection :

Pre-requisites :

  • Java 8 or above
  • Spring framework 5.0.0 RELEASE (5.X)
  • Eclipse IDE (spring sts-3.9.X.RELEASE IDE is preferred)
  • Apache maven as the build tool

You can find the total project implementation in my github repository. Check it out through this url :

https://github.com/vgunawardana/springXML

First, create a Maven Project and select an archetype(or you can simply skip it by creating a simple project). Then provide suitable group Id and artifact Id. (Hope you know basics of maven :)). You’ll be ended up getting the following project hierarchy.

Figure 1 : Maven Project Structure

As you can see Java compiler is set to J2SE -1.5 , (which is default to IDE ), you have to set it to Java 8. It can be done by adding maven compiler plugin.

Also, we have to download all the spring dependencies through maven, to our project to work with. So the Figure 2 represents, how you can add both plugin and dependency right into your project.

Figure 2 : pom.xml file

This is a layered application with Model, Repository and Service layers, though it does not wrap up with proper tier separations.

First, go with your real entity, create a Model Class called “CarManufacturer” and generate getters and setters for its two properties.

Figure 3 : CarManufacturer Model Class

Next move is to create Repository class and extract its interface out of that. This is actually the data layer of the application and it has been implemented a method to extract all the car manufactures as an ArrayList.

Figure 4 : Repository Implementation class
Figure 5 : Extracted interface from the implementation

Then move to the Service Class which will be accessed by the application test class directly. First, I would like to go with the typical J2EE manner (Figure 6) , before dressing it up with Spring Framework. You can apparently see, service is tightly bound to the data layer implementation. And all the data is hard-coded in that repository class (Figure 4). That is where Spring can help us :)

Figure 6 : Service Implementation Class without Spring

Let’s dress up this service class with Spring. There are two types of injections we can use ;

  • Setter Injection
  • Constructor Injection

In this tutorial , I have only focused on the Setter Injection.

Figure 7 : Service Implementation Class with Spring

So as in the Figure 7, you have to delete direct object initialization to its interface and generate a setter method relevant to that property. Next extract the interface out of this Service Implementation class. (CarManufacturerServiceImpl)

Figure 8 : CarManufacturerService Interface

Then you have to come up with the applicationContext.xml file which is the root file for all the configurations of spring to be in action. The name can be anything. But as the standard, applicationContext.xml is preferred. You have to address all the bean definitions and dependencies in here. Add your configuration file under src>main >resources conventionally. manufacturer property in CarManufacturerServiceImpl, will be wired by Spring automatically, to its reference class through the setter method declared in CarManufacturerServiceImpl.

Figure 9 : applicationContext.xml File

Finally it is time to write Application class with the main method to test our whole application.

So if it were not along with Spring, you would have written the code by using “program to interface not for object” concept. Refer Figure 10 implementation. Yeah, you can say it’s a good approach since it will definitely lead removing unnecessary coupling between classes. But still, the interface is tightly bound to its implementation. So Spring approach is better to be considered.

Figure 10 : ApplicationTest class without Spring

Now it’s time to write the code with Spring approach. The bean definitions in applicationContext.xml (as in Figure 9) will replace,new keyword which will lead to hard-coded interface references.

Figure 11 : Application Test class with Spring

Above ClassPathXmlApplicationContext in Application.java(Figure 11) will find the class path of the implemented spring configuration file, passed through a string value ( applicationContext.xml ) which will allow us to access its defined beans. Since we use Maven as the build tool, it will compile our file into the application root directory, so we don’t need to give any specific path for that.

Hope you guys could clearly understand the concept behind the Spring Setter injection using xml configuration and how it goes in action. This will really help to keep separated all our configurations from the business logic, so that you can easily focus only on your business implementation. Also, this will be a big advantage when you really need to change the environment say, from development to testing in big projects, since you don’t need to recompile your code repetitively at all.

Find the total project implementation at github :

https://github.com/vgunawardana/springXML

--

--