SpringCustomQualifierAnnotation

编程

Contents

  • Custom Qualifier with Bean Meta Tag and Qualifier Attribute Tag in XML
  • Custom Qualifier with value() Method using Java Configuration
  • Custom Qualifier without any method using XML
  • Download Complete Source Code



 

Custom Qualifier with Bean Meta Tag and Qualifier Attribute Tag in XML

To create a custom qualifier, we need to create an annotation which will be annotated with @Qualifier annotation. Here we will define our custom methods on the basis of which we will select bean for autowiring. Find the custom qualifier.
PersonQualifier.java

package com.concretepage;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

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

@Target({ElementType.FIELD,ElementType.METHOD,

ElementType.TYPE,ElementType.PARAMETER})

@Retention(RetentionPolicy.RUNTIME)

@Qualifier

public@interfacePersonQualifier{

String status();

String quality();

}

In XML file, we use <meta> tag or qualifier <attribute> tag to define criteria to qualify for bean dependency injection. First we will provide example with <meta> tag.
app-conf-1.xml

<?xml version="1.0" encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>

<beanclass="com.concretepage.bean.PersonService"/>

<beanclass="com.concretepage.bean.Student">

<metakey="status"value="Rich"/>

<metakey="quality"value="Bad"/>

<propertyname="personName"value="Student Kansh"/>

</bean>

<beanclass="com.concretepage.bean.Teacher">

<metakey="status"value="Poor"/>

<metakey="quality"value="Good"/>

<propertyname="personName"value="Teacher Sudama"/>

</bean>

</beans>

Now if we want to use <attribute> tag of <qualifier> tag, we do as follows.
app-conf-2.xml

<?xml version="1.0" encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>

<beanclass="com.concretepage.bean.PersonService"/>

<beanclass="com.concretepage.bean.Student">

<qualifiertype="PersonQualifier">

<attributekey="status"value="Rich"/>

<attributekey="quality"value="Bad"/>

</qualifier>

<propertyname="personName"value="Student Kansh"/>

</bean>

<beanclass="com.concretepage.bean.Teacher">

<qualifiertype="PersonQualifier">

<attributekey="status"value="Poor"/>

<attributekey="quality"value="Good"/>

</qualifier>

<propertyname="personName"value="Teacher Sudama"/>

</bean>

</beans>

Now find the service class where will use our custom qualifier.
PersonService.java

package com.concretepage.bean;

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

import com.concretepage.PersonQualifier;

publicclassPersonService{

@Autowired

@PersonQualifier(status="Poor", quality="Good")

privatePerson person;

publicPerson getPerson(){

return person;

}

publicvoid setPerson(Person person){

this.person = person;

}

}

The field person will be autowired using the bean which follow the criteria defined by @PersonQualifier and this will be Teacher class in our case.

Find other classes used in our example.
Person.java

package com.concretepage.bean;

publicclassPerson{

privateString personName;

publicString getPersonName(){

return personName;

}

publicvoid setPersonName(String personName){

this.personName = personName;

}

}

Student.java

package com.concretepage.bean;

publicclassStudentextendsPerson{

privateString stdLocation;

publicString getStdLocation(){

return stdLocation;

}

publicvoid setStdLocation(String stdLocation){

this.stdLocation = stdLocation;

}

}

Teacher.java

package com.concretepage.bean;

publicclassTeacherextendsPerson{

privateString subject;

publicString getSubject(){

return subject;

}

publicvoid setSubject(String subject){

this.subject = subject;

}

}

SpringDemo.java

package com.concretepage;

import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.concretepage.bean.PersonService;

publicclassSpringDemo{

publicstaticvoid main(String[] args){

AbstractApplicationContext context =newClassPathXmlApplicationContext("app-conf-1.xml");

PersonService service = context.getBean(PersonService.class);

System.out.println(service.getPerson().getPersonName());

context.close();

}

}

Find the output.

TeacherSudama

Test the application using file app-conf-2.xml, we will get the same result.

 

 

Custom Qualifier with value() Method using Java Configuration

Here we will create a custom qualifier with value() method. For the example we are creating an enum for some constant value. We will also create two components that will be annotated by custom qualifier at class level. Find the example using java configuration
AnimalQualifier.java

package com.concretepage;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

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

@Target({ElementType.FIELD,ElementType.METHOD,

ElementType.TYPE,ElementType.PARAMETER})

@Retention(RetentionPolicy.RUNTIME)

@Qualifier

public@interfaceAnimalQualifier{

AnimalType value();

}

AnimalType.java

package com.concretepage;

publicenumAnimalType{

DEER, FOX

}

AppConfig.java

package com.concretepage;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

@Configuration

@ComponentScan(basePackages="com.concretepage.bean")

publicclassAppConfig{

}

Animal.java

package com.concretepage.bean;

publicinterfaceAnimal{

void printName();

}

Deer.java

package com.concretepage.bean;

import org.springframework.stereotype.Component;

import com.concretepage.AnimalQualifier;

import com.concretepage.AnimalType;

@Component

@AnimalQualifier(AnimalType.DEER)

publicclassDeerimplementsAnimal{

@Override

publicvoid printName(){

System.out.println("--- Deer ---");

}

}

Fox.java

package com.concretepage.bean;

import org.springframework.stereotype.Component;

import com.concretepage.AnimalQualifier;

import com.concretepage.AnimalType;

@Component

@AnimalQualifier(AnimalType.FOX)

publicclassFoximplementsAnimal{

@Override

publicvoid printName(){

System.out.println("--- Fox ---");

}

}

AnimalService.java

package com.concretepage.bean;

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

import org.springframework.stereotype.Service;

import com.concretepage.AnimalQualifier;

import com.concretepage.AnimalType;

@Service

publicclassAnimalService{

@Autowired

@AnimalQualifier(AnimalType.DEER)

privateAnimal animal;

publicAnimal getAnimal(){

return animal;

}

}

SpringDemo.java

package com.concretepage;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.concretepage.bean.AnimalService;

publicclassSpringDemo{

publicstaticvoid main(String[] args){

AnnotationConfigApplicationContext ctx =newAnnotationConfigApplicationContext();

ctx.register(AppConfig.class);

ctx.refresh();

AnimalService animal = ctx.getBean(AnimalService.class);

animal.getAnimal().printName();

ctx.close();

}

}

Find the output.

---Deer---

 

 

Custom Qualifier without any method using XML

Custom qualifier can also be created without any method, too. For the demo, we have created custom qualifiers @FoxQualifier and @DeerQualifier without any method declaration. In our service class, the constructor argument has been marked with custom qualifier annotation with @Autowired.
DeerQualifier.java

package com.concretepage;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

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

@Target({ElementType.FIELD,ElementType.METHOD,

ElementType.TYPE,ElementType.PARAMETER})

@Retention(RetentionPolicy.RUNTIME)

@Qualifier

public@interfaceDeerQualifier{

}

FoxQualifier.java

package com.concretepage;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

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

@Target({ElementType.FIELD,ElementType.METHOD,

ElementType.TYPE,ElementType.PARAMETER})

@Retention(RetentionPolicy.RUNTIME)

@Qualifier

public@interfaceFoxQualifier{

}

AnimalService.java

package com.concretepage.bean;

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

import org.springframework.stereotype.Service;

import com.concretepage.FoxQualifier;

@Service

publicclassAnimalService{

privateAnimal animal;

@Autowired

publicAnimalService(@FoxQualifierAnimal animal){

this.animal = animal;

}

publicAnimal getAnimal(){

return animal;

}

}

app-conf.xml

<?xml version="1.0" encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>

<beanclass="com.concretepage.bean.AnimalService"/>

<beanclass="com.concretepage.bean.Fox">

<qualifiertype="FoxQualifier"/>

</bean>

<beanclass="com.concretepage.bean.Deer">

<qualifiertype="DeerQualifier"/>

</bean>

</beans>

Animal.java

package com.concretepage.bean;

publicinterfaceAnimal{

void printName();

}

Deer.java

package com.concretepage.bean;

publicclassDeerimplementsAnimal{

@Override

publicvoid printName(){

System.out.println("--- Deer ---");

}

}

Fox.java

package com.concretepage.bean;

publicclassFoximplementsAnimal{

@Override

publicvoid printName(){

System.out.println("--- Fox ---");

}

}

SpringDemo.java

package com.concretepage;

import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.concretepage.bean.AnimalService;

publicclassSpringDemo{

publicstaticvoid main(String[] args){

AbstractApplicationContext context =newClassPathXmlApplicationContext("app-conf.xml");

AnimalService service = context.getBean(AnimalService.class);

service.getAnimal().printName();

context.close();

}

}

Find the output.

---Fox---


Now I am done. Happy spring learning!

以上是 SpringCustomQualifierAnnotation 的全部内容, 来源链接: utcz.com/z/513101.html

回到顶部