第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

Spring @Qualifier 注釋

標簽:
Spring

Spring @Qualifier 注释

可能会有这样一种情况,当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除混乱。下面显示的是使用 @Qualifier 注释的一个示例。

示例

让我们使 Eclipse IDE 处于工作状态,请按照下列步骤创建一个 Spring 应用程序:

步骤描述
1创建一个名为 SpringExample 的项目,并且在所创建项目的 src 文件夹下创建一个名为 com.tutorialspoint 的包。
2使用 Add External JARs 选项添加所需的 Spring 库文件,就如在 Spring Hello World Example 章节中解释的那样。
3在 com.tutorialspoint 包下创建 Java 类 StudentProfile 和 MainApp
4在 src 文件夹下创建 Beans 配置文件 Beans.xml
5最后一步是创建所有 Java 文件和 Bean 配置文件的内容,并且按如下解释的那样运行应用程序。

这里是 Student.java 文件的内容:

package com.tutorialspoint; public class Student {    private Integer age;    private String name;    public void setAge(Integer age) {       this.age = age;    }       public Integer getAge() {       return age;    }    public void setName(String name) {       this.name = name;    }      public String getName() {       return name;    } }

这里是 Profile.java 文件的内容:

package com.tutorialspoint; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class Profile {    @Autowired    @Qualifier("student1")    private Student student;    public Profile(){       System.out.println("Inside Profile constructor." );    }    public void printAge() {       System.out.println("Age : " + student.getAge() );    }    public void printName() {       System.out.println("Name : " + student.getName() );    } }

下面是 MainApp.java 文件的内容:

package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {    public static void main(String[] args) {       ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");       Profile profile = (Profile) context.getBean("profile");       profile.printAge();       profile.printName();    } }

考虑下面配置文件 Beans.xml 的示例:

<?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-3.0.xsd     http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <context:annotation-config/>    <!-- Definition for profile bean -->    <bean id="profile" class="com.tutorialspoint.Profile">    </bean>    <!-- Definition for student1 bean -->    <bean id="student1" class="com.tutorialspoint.Student">       <property name="name"  value="Zara" />       <property name="age"  value="11"/>    </bean>    <!-- Definition for student2 bean -->    <bean id="student2" class="com.tutorialspoint.Student">       <property name="name"  value="Nuha" />       <property name="age"  value="2"/>    </bean> </beans>


點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學(xué)

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消