Spring 5 Setter Dependency Injection + Java Configuration

In previous post, Setter Dependency Injection using XML Configuration was explained. In this post Setter Dependency Injection using Java Configuration is explained.

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

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

Setter Injection Java Configuration With Example

1. Create a POJO Class(es) by specifying dependent Object

File : GalleryAppSetterInjection.java

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

public class GalleryAppSetterInjection {
	private IShareSetterInjection share;

	public IShareSetterInjection getShare() {
		return share;
	}

	public void setShare(IShareSetterInjection share) {
		this.share = share;
	}
	
}


2. Create an Interface/Class and its Implementation class

IShareSetterInjection is a dependent for GalleryAppSetterInjection class. GmailSetterInjection and WatsappSetterInjection are its implementation.

File : IShareSetterInjection.java

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

public interface IShareSetterInjection {

	void send();

}

File : GmailSetterInjection.java

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

public class GmailSetterInjection implements IShareSetterInjection {

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

}

File : WatsAppSetterInjection.java

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

public class WatsAppSetterInjection implements IShareSetterInjection {

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

}

3. Create a configuration file

Specify the beans using Java Configuration file and inject the dependency object in GalleryAppSetterInjection class. Spring will identify this as a Configuration file by identifying the meta data i.e @Configuration>annotation at class level. Spring will the instance for the class which is annotated with @Bean

File : AppConfig5SetterInjection.java

package com.learnfromexamples.springcore.configuration;

import org.springframework.context.annotation.Bean;
....
@Configuration
public class AppConfig5SetterInjection {
	
	@Bean
	public GalleryAppSetterInjection getGalleryApp() {
		GalleryAppSetterInjection app = new GalleryAppSetterInjection();
		app.setShare(getWatsApp());
		return app;
	}
	
	
	@Bean
	public IShareSetterInjection getEmail() {
		IShareSetterInjection share = new GmailSetterInjection();
		return share;
	}
	
	@Bean
	public IShareSetterInjection getWatsApp() {
		IShareSetterInjection share = new WatsAppSetterInjection();
		return share;
	}
}

 

4. Create a Main Class

File : App3SetterInjection.java

package com.learnfromexamples.springcore.JavaConfiguration;

import org.springframework.context.ApplicationContext;
....
public class App3SetterInjection {
	public static void main(String[] args) {

		@SuppressWarnings("resource")
		ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig5SetterInjection.class);

		GalleryAppSetterInjection app = context.getBean(GalleryAppSetterInjection.class);
		app.getShare().send();
	}
}

 

5. Output

Hello WatsApp !!!

6. Code

Spring Setter Dependency Injection - Java Configuration
Spring Setter Dependency Injection – Java Configuration

Add a Comment

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