Wednesday 21 October 2020

How to use @Qualifier and @Primary Annotations in Spring


@Qualifier Annotation
Due to spring development there may be a situation when you create more than one bean of the same type and want to wire only one of them with a property. In such cases, you can use the @Qualifier annotation along with @Autowired to remove the confusion by specifying which exact bean will be wired.

@Primary Annotation
There may be a situation when you create more than one bean of the same type and want to wire only one of them with a property , and not want to use @Qualifier Annotation then you can Annotate any one bean as @Primary .
This annotation can be used on any class directly or indirectly annotated with the @Component annotation or on methods annotated with the @Bean annotation.

Project Code Structure



Create pom.xml file 

<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.ramsis</groupId>

  <artifactId>springQualifierDemo</artifactId>

  <version>0.0.1-SNAPSHOT</version>



<properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <springframework.version>5.1.5.RELEASE</springframework.version>

    

    <maven.compiler.source>1.8</maven.compiler.source>

    <maven.compiler.target>1.8</maven.compiler.target>

  </properties>


  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

        <dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${springframework.version}</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>${springframework.version}</version>

</dependency>

  </dependencies>


</project>


-------------------------------------------------------------------------------------------- 


Create CompanyInfo Class


package com.ramsis.bean;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Component;


@Component

public class CompanyInfo {


@Autowired

@Qualifier("hyundai")

CompanyType companytype;

public void getInfo() {

companytype.getCompanyType();

}

}


--------------------------------------------------------------------------------------------


Create CompanyType Class


package com.ramsis.bean;


public interface CompanyType {


public void getCompanyType();

}


--------------------------------------------------------------------------------------------


Create Hyundai Class



package com.ramsis.bean;


import org.springframework.stereotype.Component;


@Component

public class Hyundai implements CompanyType {


@Override

public void getCompanyType() {

        System.out.println("Hyundai making Automobiles.");


}


}


--------------------------------------------------------------------------------------------


Create Nokia Class


package com.ramsis.bean;


import org.springframework.stereotype.Component;


@Component

public class Nokia implements CompanyType {


@Override

public void getCompanyType() {

  System.out.println("Nokia making Mobile Phones.");


}


}


--------------------------------------------------------------------------------------------


Create Reymond Class



package com.ramsis.bean;


import org.springframework.context.annotation.Primary;

import org.springframework.stereotype.Component;


@Component

@Primary

public class Reymond implements CompanyType {


@Override

public void getCompanyType() {

        System.out.println("Reymonds making fabrics and fashion products.");

}


}


--------------------------------------------------------------------------------------------


Create Runner Class


package com.ramsis.Main;


import org.springframework.context.annotation.AnnotationConfigApplicationContext;


import com.ramsis.bean.CompanyInfo;

import com.ramsis.config.Config;


public class Runner {

public static void main(String args[]) {

AnnotationConfigApplicationContext  context = new      AnnotationConfigApplicationContext(Config.class);

CompanyInfo info =context.getBean("companyInfo",CompanyInfo.class);

info.getInfo();

context.close();

}

}


--------------------------------------------------------------------------------------------


Create Config Class


package com.ramsis.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan(basePackages="com.ramsis.bean")
public class Config {

}


--------------------------------------------------------------------------------------------


After run this project you will get the output as below mention image










No comments:

Post a Comment