Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Spring @Qualifier with Constructors

In this topic, we are using the @Qualifier annotation with a constructor to specify the dependency instance. We can specify the @Qualifier annotation on individual constructor arguments or method parameters, as shown in the following example:

Let's create a spring project, this project is a Maven-Based Spring Project and contains the following files.

  • App.java

  • AppConfig.java

  • Award.java

  • BookerAward.java

  • NationalAward.java

  • PulitzerAward.java

  • FictionWriter.java

  • TechnicalWriter.java

  • Writer.java

  • pom.xml

Project Structure:

Files Source Code:

//App.java

This file contains the code to create an IOC container for our application. The AnnotationConfigApplicationContext class is used to create an object for application context.

package com.studytonight.community;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
	public static void main(String[] args) {
		
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		Writer writer = context.getBean("fictionWriter", Writer.class);
		writer.write();
		writer.getAward();
		// Close the context
		context.close();
	}
}

// AppConfig.java

This is a configuration file in Java which is an alternate of the applicationContext.xml file that we created for the XML-based configuration example. The @Configuration annotation indicates that this is not a simple class but a configuration class and the @ComponentScan annotation is used to indicate the component's location in our spring project.

package com.studytonight.community;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.studytonight.community")
public class AppConfig {

}

// Award.java

This is an interface Award that contains a award() abstract method. Each class that implements this interface will have to override the award() method.

package com.studytonight.community;
public interface Award {
	void award();

}

// BookerAward.java

This is a component class that is marked using the @Component annotation and implements the Award interface.

package com.studytonight.community;
import org.springframework.stereotype.Component;
@Component
public class BookerAward implements Award {
	@Override
	public void award() {
		
		System.out.println("You got booker prize...");
	}
}

// FictionWriter.java

This is another component class that is marked using the @Component annotation and implements the Writer interface.

package com.studytonight.community;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class FictionWriter implements Writer {
	private Award award;
		
	@Autowired
	public FictionWriter(@Qualifier("bookerAward") Award award) {
		this.award = award;
	}
	@Override
	public void write() {
		
		System.out.println("Write Fiction Novels...");
	}
	@Override
	public void getAward() {
		
		award.award();
	}
}

// NationalAward.java

This is another component class that is marked using the @Component annotation and implements the Award interface.

package com.studytonight.community;
import org.springframework.stereotype.Component;

@Component
public class NationalAward implements Award{	
	public void award() {		
		System.out.println("Your Writting got National Award!");
	}
}

// PulitzerAward.java

This is another component class that is marked using the @Component annotation and implements the Award interface.

package com.studytonight.community;
import org.springframework.stereotype.Component;
@Component
public class PulitzerAward implements Award {

	@Override
	public void award() {
		
		System.out.println("You have won Pulitzer Award.");
	}
}

// TechnicalWriter.java

This is another component class that is marked using the @Component annotation and implements the Writer interface.

package com.studytonight.community;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class TechnicalWriter implements Writer{

	private Award award;

	@Autowired
	public TechnicalWriter(@Qualifier("pulitzerAward") Award award) {
		this.award = award;
	}
	@Override
	public void write() {
		
		System.out.println("Write technical stuffs...");
	}
	@Override
	public void getAward() {
		award.award();		
	}
}

// Writer.java

package com.studytonight.community;
public interface Writer {
	
	void write();
	void getAward();
}

// pom.xml

This file contains all the dependencies of this project such as spring jars, servlet jars, etc. Put these dependencies into your project to run the application.

<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.studytonight</groupId>
  <artifactId>springproject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
  <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
	</dependencies>
	<properties>
		<spring.version>5.2.8.RELEASE</spring.version>
	</properties>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
    
  </build>
</project>

Run the Application

After successfully updating these two files into the project run the application and you will get the output as below.


Write Fiction Novels...
You got booker prize...