国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

5、spring自動裝配之@Qualifier注解的使用

2019-11-08 02:53:32
字體:
來源:轉載
供稿:網友

在上一篇4、sPRing使用@Autowired注解實現自動裝配 教程中,我們知道了怎么使用@Autowired注解進行自動裝配。但是當存在兩個類型一致的person bean時,將會有什么情況出現。我們一起來看看下面的例子:


例子

說明:如果已經看了上一篇教程,可以直接跳到第二步

第一步:創建bean

Customer類

package com.main.autowrite.autowired.annotation;import org.springframework.beans.factory.annotation.Autowired;public class Customer { //即將自動裝配的屬性 private Person person; private String type; @Autowired public Customer(Person person) { this.person = person; } //getter and setter methods //toString methods}

Person類

package com.main.autowrite.autowired.annotation;public class Person { private String name; private int age; //getter and setter methods //toString methods}

第二步:修改beans.xml配置文件

說明:這里存在了兩個person bean,一個是person1,一個是person2

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="customer" class="com.main.autowrite.autowired.annotation.Customer" > <property name="type" value="user"></property> </bean> <bean id="person1" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="jack" /> <property name="age" value="18"/> </bean> <bean id="person2" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="tom" /> <property name="age" value="28"/> </bean></beans>

第三步:編寫測試代碼

@Test public void test(){ applicationContext context = new ClassPathXmlApplicationContext("com/main/autowrite/autowired/annotation/beans.xml"); Customer customer = (Customer)context.getBean("customer"); System.out.print(customer.toString()); }

測試結果如下:(拋出異常)

這里寫圖片描述

這是因為Customer在進行自動裝配時,不清楚要裝載person1,還是要裝載person2,這時我們就應該使用@Qualifier注解來告訴它,我們想要的是哪一個!!


為解決上述問題,這里引入@Qualifier注解

@Qualifier示例

修改你的Customer類如下:

package com.main.autowrite.autowired.annotation;import java.lang.annotation.Retention;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;public class Customer { //即將自動裝配的屬性 @Autowired @Qualifier("person2") private Person person; private String type; //getter and setter methods //toString methods}

修改你的beans.xml配置文件如下: 此時應該將引入spring上下文以及context:annotation-config標簽

<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="customer" class="com.main.autowrite.autowired.annotation.Customer" > <property name="type" value="user"></property> </bean> <bean id="person1" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="jack" /> <property name="age" value="18"/> </bean> <bean id="person2" class="com.main.autowrite.autowired.annotation.Person"> <property name="name" value="tom" /> <property name="age" value="28"/> </bean></beans>

此時Customer bean將會自動裝配person2 bean

運行結果如下:

這里寫圖片描述


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东港市| 昌乐县| 信丰县| 芜湖市| 望都县| 阳信县| 神池县| 美姑县| 顺义区| 崇明县| 东源县| 资溪县| 黎川县| 三原县| 陕西省| 自治县| 嘉峪关市| 剑川县| 阳江市| 绵阳市| 嘉义县| 缙云县| 大方县| 岫岩| 灯塔市| 松原市| 新余市| 高碑店市| 盐城市| 锡林郭勒盟| 镇赉县| 铜鼓县| 克什克腾旗| 辽宁省| 湘潭县| 合山市| 富民县| 上蔡县| 高唐县| 北宁市| 大关县|