日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

中國(guó)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2

wkspring教程

Spring @Qualifier 注釋

閱讀 (2165)

Spring @Qualifier 注釋

可能會(huì)有這樣一種情況,當(dāng)你創(chuàng)建多個(gè)具有相同類(lèi)型的 bean 時(shí),并且想要用一個(gè)屬性只為它們其中的一個(gè)進(jìn)行裝配,在這種情況下,你可以使用 @Qualifier 注釋和 @Autowired 注釋通過(guò)指定哪一個(gè)真正的 bean 將會(huì)被裝配來(lái)消除混亂。下面顯示的是使用 @Qualifier 注釋的一個(gè)示例。

示例

讓我們使 Eclipse IDE 處于工作狀態(tài),請(qǐng)按照下列步驟創(chuàng)建一個(gè) Spring 應(yīng)用程序:

步驟 描述
1 創(chuàng)建一個(gè)名為 SpringExample 的項(xiàng)目,并且在所創(chuàng)建項(xiàng)目的 src 文件夾下創(chuàng)建一個(gè)名為 com.tutorialspoint 的包。
2 使用 Add External JARs 選項(xiàng)添加所需的 Spring 庫(kù)文件,就如在 Spring Hello World Example 章節(jié)中解釋的那樣。
3 com.tutorialspoint 包下創(chuàng)建 Java 類(lèi) StudentProfileMainApp
4 src 文件夾下創(chuàng)建 Beans 配置文件 Beans.xml
5 最后一步是創(chuàng)建所有 Java 文件和 Bean 配置文件的內(nèi)容,并且按如下解釋的那樣運(yùn)行應(yīng)用程序。

這里是 Student.java 文件的內(nèi)容:

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 文件的內(nèi)容:

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 文件的內(nèi)容:

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>

一旦你在源文件和 bean 配置文件中完成了上面兩處改變,讓我們運(yùn)行一下應(yīng)用程序。如果你的應(yīng)用程序一切都正常的話(huà),這將會(huì)輸出以下消息:

Inside Profile constructor.
Age : 11
Name : Zara
關(guān)閉
程序員人生
主站蜘蛛池模板: 尤物精品 | 国产 欧美 另类 | 亚洲视频在线一区 | 久久综合影视 | 久久久久久久国产 | 99re在线视频精品 | 欧美三区| 久久久国产精品视频 | 欧美一区二区三区视频 | 91精品国产99久久久久久红楼 | 黄色一级大片在线观看 | 国产精品无码永久免费888 | 亚洲 欧美 日韩 综合 | 国产精品福利在线 | 日本在线视频观看 | www.亚洲一区| 亚洲一区二区免费电影 | 亚洲综合另类 | 国产精品高清在线 | 国产精品区视频 | 国产又爽又黄免费视频 | 精品久久a | 在线日韩一区 | 日韩精品中文字幕在线 | 日韩视频免费在线 | 偷拍亚洲| aaaa毛片| 在线观看欧美激情 | 亚洲影院一区 | 欧美精品一区在线观看 | 精品福利视频一区二区三区 | 91视频国产一区 | 一区二区日韩精品 | 午夜精品久久久久99蜜 | av久色 | 欧美日韩国产精品 | 欧美一区二区人人喊爽 | 夜夜爽爽爽久久久久久魔女 | 免费观看日韩毛片 | 精品国产第一国产综合精品 | 欧美一区二区国产 |