spring bean註冊和注入覆蓋

2021-09-10 22:54:05 字數 690 閱讀 4654

1.註冊

//儲存註冊的俄beandefinition  

private final mapbeandefinitionmap = new concurrenthashmap();  

//向ioc容器註冊解析的beandefiniton  

public void registerbeandefinition(string beanname, beandefinition beandefinition)  

throws beandefinitionstoreexception  

catch (beandefinitionvalidationexception ex)  

}  //註冊的過程中需要執行緒同步,以保證資料的一致性  

synchronized (this.beandefinitionmap)  

else  

}  }  

//ioc容器中沒有已經註冊同名的bean,按正常註冊流程註冊  

else  

this.beandefinitionmap.put(beanname, beandefinition);  

//重置所有已經註冊過的beandefinition的快取  

resetbeandefinition(beanname);  

}  }

2.注入

參考:

Spring Bean注入與獲取

屬性注入即通過set 方法注入bean的屬性值或依賴物件,由於屬性注入方式具有可選擇性和靈活性高的優點,因此屬性注入是實際應用中最常採用的注入方式。屬性注入要求bean提供乙個預設的建構函式,並為需要注入的屬性提供對應的setter方法。spring先呼叫bean的預設建構函式例項化bean物件,然...

Spring bean注入的方式

一 setter方法注入 配置檔案如下 action實現類中 private ihelloservice helloservice private string name public void sayhello public void sethelloservice ihelloservice h...

04 Spring Bean注入方式

實現ioc容器主要有兩種方式 1,依賴查詢 2,依賴注入,spring採用的是依賴注入的方式 依賴注入又分為3種方式 2.1 構造器注入 2.2 setter注入 2.3 介面注入 構造器注入和setter注入背後都是採用反射的技術來實現。介面注入,通常是指資源並非來自系統自身,而是來自系統外。比如...