Quick Inquiry

Home >> Articles >> How To Implement Dependency Injection In Spring – Java

Aegis Java web development team is all set to share this tutorial with global java web development community to make developers efficient in understanding how spring manages the several objects using beans configuration. With the help of simple java code snippet (written in the post), you can test the configuration used in this post.

Java web application generally does not consist of a single object, it always will have a multiple objects depending on each other to achieve some output. Even a simple java application will have few objects that works together to generate some output.

Here in this section we will see how spring manages the number of objects using beans configuration.


Dependency Injection / IoC

Don't make yourself confuse here, IoC (Inversion of Control) and DI (Dependency Injection) both are nothing but a same things. We refer to the same configuration calling it by two different names.

In simple word DI is a process that let the user free from worrying about new keyword. Don't need to take care of new keyword in your project to initialize a depending object, let the spring DI manage this things.

Let’s jump in to the visible example to understand this concept rather than confusing yourself with theoretical concepts.


Example of Java Class having a depending Objects

ContactModel.java


package com.aegis.di.demo; public class ContactModel { long id; String address; String mobileNo; String emailId; } PersonModel.java package com.aegis.di.demo; public class PersonModel { private long id; private String name; private String designation; private ContactModel contact; PersonModel() { contact = new ContactModel(); contact.setMobileNo("123456789"); contact.setEmailId("abc@abc.com"); } }

As we can see the above, PersonModel object contains dependent object ContactModel and we initialize it through new keyword in constructor of PersonModel class.

Here Spring comes into the picture to resolve above dependency there are two types of dependency injections available.

1) Constructor-based dependency injection

Constructor-based DI is accomplished by the container to invokes a class constructor with matching number of constructor arguments.

Lets consider above class.

ContactModel.java


package com.aegis.di.demo; public class ContactModel { private long id; private String address; private String mobileNo; private String emailId; ContactModel(String address, String mobileNo, String emailId) { this.address = address; this.mobileNo = mobileNo; this.emailId = emailId; } }

Below is the spring configuration file that has been declared using constructor-args for the above class.


Config.xml


<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- Definition for ContactModel bean --> <bean id="contactModel" class="com.aegis.di.demo.ContactModel"> <constructor-arg index="0" value="Mumbai"/> <constructor-arg index="1" value="9999912345"/> <constructor-arg index="2" value="abc@aegis.com"/> </bean> </beans>

Now lets add one more class that have the dependent object for the constructor based DI


PersonModel.java


package com.aegis.di.demo; public class PersonModel { private long id; private String name; private String designation; private ContactModel contact; PersonModel(String name, String designation, ContactModel contact) { this.name = name; this.designation = designation; this.contact = contact; } }

Here we will consider ContactModel same as above.


Config.xml


<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- Definition for ContactModel bean --> <bean id="contactModel" class="com.aegis.di.demo.ContactModel"> <constructor-arg index="0" value="Mumbai" /> <constructor-arg index="1" value="9999912345" /> <constructor-arg index="2" value="abc@aegis.com" /> </bean> <bean id="personModel" class="com.aegis.di.demo.PersonModel"> <constructor-arg index="0" value="John" /> <constructor-arg index="1" value="Software Engineer" /> <constructor-arg index="2" ref="contactModel" /> </bean> </beans>

As you can see the fact we provided the reference of the bean and excluded use of new keyword, spring itself will manage the same.


2) Setter-based dependency injection

Setter-based DI is accomplished by the container to invoke setter methods of POJO class after default (no-args) constructor.

Lets have an example to understand it better.

ContactModel.java


package com.aegis.di.demo; public class ContactModel { private long id; private String address; private String mobileNo; private String emailId; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getMobileNo() { return mobileNo; } public void setMobileNo(String mobileNo) { this.mobileNo = mobileNo; } public String getEmailId() { return emailId; } public void setEmailId(String emailId) { this.emailId = emailId; } }

As you can see we removed the parameterized constructor and add getter/setter method to the pojo.


Config.xml


<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- Definition for ContactModel bean --> <bean id="contactModel" class="com.aegis.di.demo.ContactModel"> <property name="address" value="Mumbai" /> <property name="mobileNo" value="9999912345" /> <property name="emailId" value="abc@aegis.com" /> </bean> </beans>

You should note the difference in config.xml file between constructor-based injection and setter-based injection.

The only difference is the element <constructor-arg> for constructor-based injection and <property> for setter-based injection.

Second most important point to be noted here is in case of initializing dependent object, it is required to use ref attribute of <property> tag.

Lets have an example adding one more class to out structure.


PersonModel.java


package com.aegis.di.demo; public class PersonModel { private long id; private String name; private String designation; private ContactModel contact; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public ContactModel getContact() { return contact; } public void setContact(ContactModel contact) { this.contact = contact; } }

Please note the difference; we have ContactModel as a child object. Please see the configuration for such scenario.


Config.xml


<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- Definition for ContactModel bean --> <bean id="contactModel" class="com.aegis.di.demo.ContactModel"> <property name="address" value="Mumbai" /> <property name="mobileNo" value="9999912345" /> <property name="emailId" value="abc@aegis.com" /> </bean> <bean id="personModel" class="com.aegis.di.demo.PersonModel"> <property name="name" value="John" /> <property name="designation" value="Software Engineer" /> <property name="contact" ref="contactModel" /> </bean> </beans>

Once done with the above concept here I would like to share simple java code snippet to test the above configuration.


package com.aegis.di.demo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestIOCBeans { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "Config.xml"); PersonModel personModel = (PersonModel) context.getBean("personModel"); ContactModel contact = personModel.getContact(); System.out.println(contact.getEmailId()); } }

DONE... Just have to resolve the dependencies by including jars.

Hope this tutorial has helped you in understanding the use of spring for managing number of objects with beans configuration in java web development. You can ask anything related to this post from our java web development team that ensures flawless response to commenter.

For further information, mail us at info@aegissofttech.com


Copyright © 2017 - aegisisc, All rights reserved