Spring 5 Setter Dependency Injection + XML Configuration

In this post, Setter Dependency Injection is explained with few examples. In this Spring Setter Dependency Injection – XML Configuration is used.

Technologies and Tools Used for Spring Setter Dependency Injection – XML Configuration

To know about technologies and tools used for this example, click here.

Spring Framework is most popular for its Dependency Injection Design Pattern. Dependency injection is identifying and creation of dependent objects and providing it to the class by different ways. Two major ways are

  • Setter Injection
  • Constructor Injection

Setter Injection in Spring

1. Create a POJO Class and specify its dependent object

File : GalleryApp.java

package com.learnfromexamples.springcore.session2.DI.episode1.SetterInjection;

public class GalleryApp {
	private IShare share;

	public IShare getShare() {
		return share;
	}

	// DI by Setter Method
	public void setShare(IShare share) {
		this.share = share;
	}
	
}

 

3. Create a Dependendent Interface/class and its implementation classes

GalleryApp is having dependency of IShare. IShare is an interface; WatsApp and Gmail are its implementations. Either WatsApp or Gmail instance should be injected into GalleryApp class.

File : IShare.java


package com.learnfromexamples.springcore.session2.DI.episode1.SetterInjection;

public interface IShare {

     public void send();

}

File : Gmail.java

package com.learnfromexamples.springcore.session2.DI.episode1.SetterInjection;

public class Gmail implements IShare  {

	public void send() {
		System.out.println("Hello Gmail !!!");		
	}
}

File : WatsApp.java

package com.learnfromexamples.springcore.session2.DI.episode1.SetterInjection;

public class WatsApp implements IShare {

	public void send() {
		System.out.println("Hello Watsapp !!!");
	}

}

 

4. Create Spring Configuration

Specify the beans in Configuration xml and inject either gmail/watapp bean into GalleryApp bean using a property tag , ref attribute.

File : beans5setterinjection.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.xsd">

	<bean id="gmail" class="com.learnfromexamples.springcore.session2.DI.episode1.SetterInjection.Gmail"/>
	<bean id="watsapp" class="com.learnfromexamples.springcore.session2.DI.episode1.SetterInjection.WatsApp"/>
	
	<bean id="galley" class="com.learnfromexamples.springcore.session2.DI.episode1.SetterInjection.GalleryApp">
 		<property name="share" ref="watsapp"> -->
		</property>
	</bean>
</beans> 

5. Create Main Class

Load the configuration file (beans5setterinjection.xml) and run it.

File : XMLConfiguration3SI.java


package com.learnfromexamples.springcore.XMLConfiguration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.learnfromexamples.springcore.session2.DI.episode1.SetterInjection.GalleryApp;

public class XMLConfiguration3SI {

	public static void main(String[] args) {

		@SuppressWarnings("resource")
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans5setterinjection.xml");

		GalleryApp app = ctx.getBean(GalleryApp.class);

		app.getShare().send();

		GalleryApp app1 = (GalleryApp)ctx.getBean("galley");

		app1.getShare().send();

	}
}

6. Output

Hello Watsapp !!!

Hello Watsapp !!!

7. Modify the dependency and Execute it

Modify the dependency in Configuration file by injecting gmail bean into gallery app.

File : beans5setterinjection.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.xsd">

	<bean id="gmail" class="com.learnfromexamples.springcore.session2.DI.episode1.SetterInjection.Gmail"/>
	<bean id="watsapp" class="com.learnfromexamples.springcore.session2.DI.episode1.SetterInjection.WatsApp"/>
	
	<bean id="galley" class="com.learnfromexamples.springcore.session2.DI.episode1.SetterInjection.GalleryApp">
<!-- 		<property name="share" ref="watsapp"> -->
<!-- 		</property> -->
		<property name="share" ref="gmail">
		</property>
	</bean>
</beans> 

8. Output

Hello Gmail !!!

Hello Gmail !!!

9. Source Code

Get the source code from the below git url.

Spring Setter Dependency Injection - XML Configuration
Spring Setter Dependency Injection – XML Configuration

Add a Comment

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