使用場景html
當你建立多個具備相同類型的 bean 時,而且想要用一個屬性只爲它們其中的一個進行裝配,在這種狀況下,你可使用 @Qualifier 註釋和 @Autowired 註釋經過指定哪個真正的 bean 將會被裝配來消除混亂。下面顯示的是使用 @Qualifier 註釋的一個示例。
實體類Rumenz/SuperRumenzjava
package com.rumenz; public class Rumenz{ private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Rumenz{" + "id=" + id + ", name='" + name + '\'' + '}'; } } package com.rumenz; public class SuperRumenz extends Rumenz { private String type; public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return "SuperRumenz{" + "type='" + type + '\'' + "} " + super.toString(); } }
自定義註解@Grp進行邏輯分組用git
package com.rumenz; import org.springframework.beans.factory.annotation.Qualifier; import java.lang.annotation.*; @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented @Qualifier public @interface Grp { }
配置Beans.xmlgithub
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"> <bean id="rumenz" class="com.rumenz.Rumenz"> <property name="id" value="123"/> <property name="name" value="入門小站"/> </bean> <bean id="superRumenz" class="com.rumenz.SuperRumenz" parent="rumenz" primary="true"> <property name="id" value="456"/> <property name="name" value="入門小站-子類"/> <property name="type" value="1"/> </bean> </beans>
當Spring容器中有多個同一種類型的Bean時,標註爲 primary="true" 的Bean將首先被注入使用
@Autowired private Rumenz rumenz; //rumenz---->SuperRumenz
調用spring
package com.rumenz; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import java.util.Arrays; import java.util.List; public class DemoApplication { @Autowired private Rumenz rumenz; @Autowired @Qualifier("rumenz") private Rumenz rumenz1; @Autowired @Qualifier private List<Rumenz> list; @Bean @Qualifier public Rumenz rumenz2(){ Rumenz r = new Rumenz(); r.setName("rumenz2"); r.setId(2); return r; } @Bean @Qualifier public Rumenz rumenz3(){ Rumenz r = new Rumenz(); r.setName("rumenz3"); r.setId(3); return r; } @Bean @Grp public Rumenz rumenz4(){ Rumenz r = new Rumenz(); r.setName("rumenz4"); r.setId(4); return r; } @Bean @Grp public Rumenz rumenz5(){ Rumenz r = new Rumenz(); r.setName("rumenz5"); r.setId(5); return r; } @Autowired @Grp private List<Rumenz> listGrp; public static void main(String[] args) { AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext(); XmlBeanDefinitionReader xr=new XmlBeanDefinitionReader(ac); xr.loadBeanDefinitions("Beans.xml"); ac.register(DemoApplication.class); ac.refresh(); DemoApplication demoApplication = ac.getBean(DemoApplication.class); System.out.println(demoApplication.rumenz.getName()); System.out.println(demoApplication.rumenz1.getName()); System.out.println(Arrays.toString(demoApplication.list.toArray())); System.out.println(Arrays.toString(demoApplication.listGrp.toArray())); ac.close(); } }
輸出ide
入門小站-子類 入門小站 [Rumenz{id=2, name='rumenz2'}, Rumenz{id=3, name='rumenz3'}, Rumenz{id=4, name='rumenz4'}, Rumenz{id=5, name='rumenz5'}] [Rumenz{id=4, name='rumenz4'}, Rumenz{id=5, name='rumenz5'}]