Spring Dependency Injection Explanation Use Loosely Coupled Objects

Java application is made up of a lot of objects. When the application becomes bigger and bigger, there are so many java objects in it, and many of them are tightly coupled together, this make your application so hard to modify.

But with Spring framework, it is so easy to manage dependencies between all those objects. This article will show you examples about how to manage java object dependencies using traditional method and Spring framework. You will know the benefits Spring framework give to you after reading.

Send Email Example

Suppose we have a java interface like below. It has a method called sendEmail().

ISendEmail

package com.dev2qa.example.sendmail;

public interface ISendEmail {
	
	public void sendEmail();

}

Below are two java classes that implement above interface.

SendEmailBySMTP : This class will send email by SMTP server.

package com.dev2qa.example.sendmail;

public class SendEmailBySMTP implements ISendEmail{

	@Override
	public void sendEmail() {
	
		System.out.println("Send email through SMTP.");
	}
}

SendEmailByLDAP : This class will send email by LDAP server.

package com.dev2qa.example.sendmail;

public class SendEmailByLDAP implements ISendEmail {

	@Override
	public void sendEmail() {

		System.out.println("Send email through LDAP. ");
	}

}

Now we will show you how to call above java object in your java code, you will find that Spring Framework can make the java object call loosely coupled extremely.

1. Call Java Object Directly
	public void sendEmailDirectly()
	{
		ISendEmail emailSender = new SendEmailBySMTP();
		emailSender.sendEmail();
	}

This is the most commonly used method. But if you want to send email use LDAP server, you have to modify, recompile and repackage above code because emailSender variable is tightly coupled with SendEmailBySMTP class. If there are a lot of such code in your application, if you want to change, it is so complex.

READ :   Spring Autowire Use @Component, @Repository, @Service and @Controller Annotations
2. Use Spring Framework

We can use Spring Framework Dependency Injection (DI) to resolve such problem easily. DI can make interface emailSender loosely coupled with the implementation class.

  1. Create a helper class SendEmailHelper
    package com.dev2qa.example.sendmail;
    
    public class SendEmailHelper {
    	
    	private ISendEmail emailSender;
    
    	public ISendEmail getEmailSender() {
    		return emailSender;
    	}
    
    	public void setEmailSender(ISendEmail emailSender) {
    		this.emailSender = emailSender;
    	}
    
    }
    
    
  2. Add beans in BeanSettings.xml as below. You can see SendEmailHelper class’s emailSender field refer to sendEmailBySMTP bean by xml configuration. So if you want to change emailSender to sendEmailByLDAP, you just need to change the xml configuration. Do not need to change java code. You can get the whole content of BeanSettings.xml file in attached download zip file.
    <bean id="sendEmailBySMTP" class="com.dev2qa.example.spring.sendmail.SendEmailBySMTP"> </bean>
    
    <bean id="sendEmailByLDAP" class="com.dev2qa.example.spring.sendmail.SendEmailByLDAP"> </bean> 
    
    <bean id="sendEmailHelper" class="com.dev2qa.example.spring.sendmail.SendEmailHelper">      
           <property name="emailSender" ref="sendEmailBySMTP"></property>
    </bean>
  3. Call above java objects via Spring.
    	public void sendEmailUseSpringFramework()
    	{
    		/* Initiate Spring Context. */
    		ApplicationContext springAppContext = new ClassPathXmlApplicationContext("BeanSettings.xml");
    
    		/* Get send email helper bean. */
    		SendEmailHelper sendEmailHelper = (SendEmailHelper)springAppContext.getBean("sendEmailHelper");
    
    		/* Get email sender and send email. */
    		sendEmailHelper.getEmailSender().sendEmail();
    	}
  4. Below is a picture about the Spring project structure, you can download the source code to see detail. You can read article How To Install Spring IDE Eclipse Plugin to learn how to create below project.
    spring loosely coupled objects example project

3. Source Code

  1. Download “SpringLooselyCoupledExamples.zip” SpringLooselyCoupledExamples.zip – Downloaded 145 times – 19 KB

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.