1. 程式人生 > >@Autowired 自動裝配

@Autowired 自動裝配

@Autowired 自動裝配

[email protected] 思維導圖

這裡寫圖片描述

[email protected] Example

2.1 建立一個USB_Interface 介面

packagecom.spring.annotation.autowired;

/**
* Created by Calvin on 2018/3/16
*/


importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.stereotype.Component;

/**
* USB 介面
*/
public interfaceUSB_Interface { /** * 驅動連線 */ voiddriverConnect(); }

2.2 實現該介面USB_Interface 中的 driverConnect()驅動連線方法

packagecom.spring.annotation.autowired;

importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;

/**
* Created by Calvin on 2018/3/16
* 實現USB
*/
@Service public classUSB_ImplimplementsUSB_Interface { /** *@Autowired自動裝配 * 作用: 在spring 2.5 引入 可以對成員變數、方法、構造方法 進行標註,來完成自動裝配 * 優點: 無需再通過傳統的在bean的xml 檔案種進行bean的注入配置 * 使用條件: * 1.根據型別進行標註的 * 2.如果出現多次同類@Autower,按照名稱進行裝配,則需要配合使用@Qualifier(在註冊bean 下,修改id 名, 也就是起別名) * 3.加入@Autowired ,必須要註冊bean ,才可以注入 */ @Autowired
privateUSBusb; public voiddriverConnect() { usb.driverMatch(); } }

2.3 USB 實體,需要註冊為Bean, 否則無法載入bean 實體,導致spring 報錯

packagecom.spring.annotation.autowired;

importlombok.Data;
importorg.springframework.stereotype.Component;
importorg.springframework.stereotype.Repository;
importorg.springframework.util.StringUtils;

/**
* Created by Calvin on 2018/3/16
* USB 實體
*/

@Data
@Component  // 註冊為Bean 實體
public classUSB {

/**
* 驅動器
*/
privateStringdriver;

/**
* 電源連線
*/
privateStringpowerConnection;

/**
* 驅動裝置
*/
public voiddriverMatch(){
System.out.println("驅動匹配中....");
this.driverConnect(driver);
}

/**
* 驅動連線
*@paramdriver
*/
private voiddriverConnect(String driver){
if(StringUtils.isEmpty(driver))
System.out.println("驅動連線失敗,請嘗試重新連線");
else
System.out.println(newStringBuffer().append(driver).append("驅動連線成功!"));
}

}

2.4 測試USB_Test , 配置spring容器(@Configurate)來註冊Bean ,通過自動掃描包下(@CompontentScan)的路徑進行Bean掃描 載入,然後進行自動裝配測試

packagecom.spring.annotation.autowired;

importorg.springframework.context.ApplicationContext;
importorg.springframework.context.annotation.AnnotationConfigApplicationContext;
importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;

/**
* Created by Calvin on 2018/3/17
*/
@Configuration
@ComponentScan({"com.spring.annotation.autowired"})
public classUSB_Test {

private static finalStringdriver="三星";

public static voidmain(String[] args) {
ApplicationContext context =newAnnotationConfigApplicationContext(USB_Test.class);
USB_Interface usb_interface = context.getBean(USB_Interface.class);
USB usb = context.getBean(USB.class);
usb.setDriver(driver);
usb_interface.driverConnect();

}
}

2.5 測試結果

三月 17, 2018 2:22:45 下午
org.springframework.context.annotation.AnnotationConfigApplicationContext
prepareRefresh 資訊: Refreshing
org.spring[email protected]246ae04d:
startup date [Sat Mar 17 14:22:45 CST 2018]; root of context hierarchy
驅動匹配中…. Disconnected from the target VM, address: ‘127.0.0.1:60115’,
transport: ‘socket’ 三星驅動連線成功!

[email protected] 流程圖

這裡寫圖片描述