In this post under Spring Core, I will show with example how to create a custom qualifier and use it with “@Component” annotation.
For our example, I will create a “@MovieGenre” custom qualifier as shown below
Custom Qualifier
package core.package45;
import org.springframework.beans.factory.annotation.Qualifier;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Qualifier
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MovieGenre {
String value();
}
As you can see in the above code, we created a custom qualifier and marked it with “@Qualifier” annotation.
The “@MovieGenre” qualifier has only one property named “value”.
Next I will show how to use it. I will create an interface “IMovie” and two implementation of it as shown below
IMovie
package core.package45;
public interface IMovie {
public void display();
}
ActionMovie
package core.package45;
import org.springframework.stereotype.Component;
@Component
@MovieGenre("action")
public class ActionMovie implements IMovie {
@Override
public void display() {
System.out.println("Action Movie");
}
}
As you can see in the above class structure, “@MovieGenre” is applied at the class level with value being “action”.
ComedyMovie
package core.package45;
import org.springframework.stereotype.Component;
@Component
@MovieGenre("comedy")
public class ComedyMovie implements IMovie {
@Override
public void display() {
System.out.println("Comedy Movie");
}
}
As you can see in the above class structure, “@MovieGenre” is applied at the class level with value being “comedy”.
Next I will show the main class for your reference.
Main class
package core.package45;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "core.package45")
public class Example45 {
@Autowired
@MovieGenre("comedy")
private IMovie movie;
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example45.class);
Example45 example45 = applicationContext.getBean(Example45.class);
example45.movie.display();;
}
}
In the above main class, I am instructing Spring framework to inject an instance of “IMovie” interface but without “@MovieGenre” annotation the Spring Framework will get confused, unable to decide which
implementation of “IMovie” to inject, as they are two implementations.
So we annotate the field “movie” with “@MovieGenre” with value “comedy” instructing the Spring framework to inject an instance of “ComedyMovie”.
In this way, we can create a custom qualifier and use it with “@Qualifier” annotation.