Skip to main content

[Spring] Spring Dependency Injection through XML Configuration


Dependency Injection 

Dependency Injection is a design pattern for providing objects that an object needs, or depends on. It can be thought of as a specific kind of Inversion of Control. (Be aware, however, the IoC container is a specific case of Dependency Injection) It separates the concerns between constructing objects and using them, and as such, the object does not need to construct the required objects itself. 

There are two ways of Dependency Injection:

  1. Constructor Injection
  2. Setter Injection

Both ways can be used in Spring for injecting dependencies.


When to use Dependency Injection?

It is especially useful when testing the application, since it does not depend on a concrete object or implementation, but only on the interface of the object.  

In the Spring framework, Spring Container is used to inject object's dependencies, meaning it does all the constructions of the objects that one depends on, and passes the constructed objects to the object that needed those objects.


Construction Injection

I'm starting from the examples that I have created in my last post about IoC. You can see the post here. With the dependency injection, I would like to make the program introduce my name after saying hello world. 

First, create an interface that the objects to be injected will implement. Here, I create a very simple interface MyName.java,

Then, create a concrete object implementing this interface.

Change the already existing interface to include the function for saying out the name.

Edit the already existing objects that implemented this interface accordingly. Also, define a constructor in each of the objects for dependency injection. 

Next, in the xml file, define the dependencies before defining beans. When defining beans, set up the constructor injection for the bean, using the dependency id as reference. 

Lastly in the main application, load the configuration file, retrieve bean from the spring container, call methods on the bean, and close the context. Here, we also call the method that depends on other objects to see how it is handled.

Running the app will result in the following:

We can see that the dependency injection has been handled by Spring through the constructor.



Setter Injection

Now we take a look at how to inject dependencies using setter methods instead of constructors.

First, create a new object that depends on other objects. Create a setter method for dependency injection in the object.


Next, define the bean and the setter injection in the xml file for this new object. One thing to notice is that the property name for the setter injection must match the name of the setter method in the object(bean) to be defined. This is because Spring automatically looks up for setter methods matching that of the property name.
For example, HelloKorean.java has a setter method setMyName(). Thus, we need to set the property name as myName to enable Spring to use setter injection properly. Be aware of the Capital cases!


Retrieve the bean from the spring container in the main app.


Running the app will give the following results.



This time, the dependency injection has been handled by Spring through the setter method.

It is also possible to inject literal values using setter methods.

Create new fields for the value to be injected, and make getter & setter methods for those values.

Set up the literal values in the xml file. Again, don't forget to make the property name match with the setter methods name.

This is the main app that I will be running. It is the same as the ones used above. Just one thing, if we were to use methods in the main app that were only in the specific object and not in the interface, we need to call that specific class, not the interface for the parameters of getBean(). Calling the methods internally does not matter.

And here is the result of running the app, the results are more customized by injecting the values.



Injection of literal values can also be done through the properties files. To do this, add the following line in the xml file:
<context:property-placeholder location="classpath:name.properties:/>

Put the properties file in the class path, and change the name into the property file name.
Then, for the property value, put ${}, and between the curly braces, put the name of the property as was defined in the properties file. It's basically like making another config file for the config file(xml). 



That was it! Now we can inject dependencies through XML files using Spring.



Comments

Popular posts from this blog

IntelliJ IDEA Error: Cannot determine path to 'tools.jar' library for 17

[Spring] Spring Inversion of Control(IoC) through XML Configuration

[Spring] Spring MVC Configuration - How to set up Spring MVC in IntelliJ