Tuesday, 7 December 2021

Spring: How to auto wire two beans implementing the same interface?

Here is the scenario, where I have an interface ‘HelloResponse’ and two classes HiServiceImpl and WelcomeServiceImpl are implementing the interface HelloService.

 

Now I want to autowire the beans of type HiServiceImpl and WelcomeServiceImpl. Since these two classes are implementing the same interface, we can use @Qualifier annotation to explicitly specify the bean that we are interested to use.

 

Step 1: Define service classes by specifying the component name explicitly.

@Component("hiService")
public class HiServiceImpl implements HelloService{

	........
	........
}

 

@Component("welcomeService")
public class WelcomeServiceImpl implements HelloService{

	........
	........

}

 

Step 2: Explicitly specify the component name using @Qualifier annotation while autowiring.

@RestController
public class HelloController {

	@Autowired
	@Qualifier("hiService")
	private HelloService hiService;

	@Autowired
	@Qualifier("welcomeService")
	private HelloService welcomeServiceImpl;

	......
	......
}

 

Find the below working application.

 

Step 1: Create new maven project ‘qualifier-demo’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

 

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>qualifier-demo</artifactId>
	<version>1</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.6</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>


	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>


	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

Step 3: Define HelloService.

 

HelloService.java

package com.sample.app.service;

public interface HelloService {
	
	public String sayHello();

}

 

Step 4: Define HiServiceImpl and WelcomeServiceImpl classes.

 

HiServiceImpl.java

package com.sample.app.service.impl;

import org.springframework.stereotype.Component;

import com.sample.app.service.HelloService;

@Component("hiService")
public class HiServiceImpl implements HelloService{

	@Override
	public String sayHello() {
		return "Hi!!!!!!!!";
	}

}

 

WelcomeServiceImpl.java

package com.sample.app.service.impl;

import org.springframework.stereotype.Component;

import com.sample.app.service.HelloService;

@Component("welcomeService")
public class WelcomeServiceImpl implements HelloService{

	@Override
	public String sayHello() {
		return "Welcome!!!!!!";
	}

}

 

Step 5: Define HelloController.

 

HelloController.java

 

package com.sample.app.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sample.app.service.HelloService;

@RestController
public class HelloController {

	@Autowired
	@Qualifier("hiService")
	private HelloService hiService;

	@Autowired
	@Qualifier("welcomeService")
	private HelloService welcomeServiceImpl;

	@GetMapping(value = "/hi", produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<HelloResponse> sayHi() {
		HelloResponse resp = new HelloResponse(hiService.sayHello());
		return ResponseEntity.ok(resp);

	}

	@GetMapping(value = "/welcome", produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<HelloResponse> sayWelcome() {
		HelloResponse resp = new HelloResponse(welcomeServiceImpl.sayHello());
		return ResponseEntity.ok(resp);

	}

	private static class HelloResponse {
		String resp;

		public HelloResponse() {
		}

		public HelloResponse(String resp) {
			super();
			this.resp = resp;
		}

		public String getResp() {
			return resp;
		}

		public void setResp(String resp) {
			this.resp = resp;
		}
		
		

	}
}

Step 6: Define main application class.

 

App.java

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
	public static void main(String[] args) {

		SpringApplication.run(App.class, args);

	}
}

Total project structure looks like below.

 

 


Run App.java

 

Open the url 'http://localhost:8080/hi' in browser, you will see below message.

{"resp":"Hi!!!!!!!!"}

 

Open the url 'http://localhost:8080/welcome' in browser, you will see below message.

{"resp":"Welcome!!!!!!"}

 


You can download the complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/rest/qualifier-demo



Previous                                                    Next                                                    Home

No comments:

Post a Comment