Spring載入Bean定義檔案筆記

2021-08-31 06:40:57 字數 879 閱讀 7138

載入xml定義檔案方式四種:

beanfactory bf=new xmlbeanfactory(new classpathresource("spring.xml"));
意思是beanfactory從xml讀取bean定義的資訊,現在bean工廠還沒有例項化bean,bean是延遲載入到bean工廠中,也就是說bean工廠會立即把bean定義的資訊載入進來,但是bean只是在被需要的時候才被例項化。

是採用的預先例項化,這樣可以及早的發現錯誤。

比如在配置檔案中出現錯誤,會立即給於提示。

從檔案系統中的xml檔案載入上下文定義資訊,

只能在指定的路徑中尋找spring.xml檔案

從類路徑中xml檔案載入上下文資訊把上下文定義的檔案當成類路徑資源,

可以在整個類路徑中尋找

從web系統中的xml檔案載入上下文資訊

載入.properties定義檔案方式

beandefinitionregistry reg=new defaultlistablebeanfactory();

propertiesbeandefinitionreader reader=new propertiesbeandefinitionreader(reg);

reader.loadbeandefinitions(new classpathresource("hellobean.properties"));

beanfactory bf=(beanfactory) reg;

spring動態載入bean

廢話不多說,直接上 private static final string strs new string protected mapremotes new hashmap for int i 0 i string name one if i 0 beandefinitionbuilder user...

spring註解定義bean

1 利用註解宣告該類 component student id名為 student public class student2 配置掃瞄器 xmlns context 掃瞄某包 多個包用逗號分隔 執行流程 spring在啟動的時候,會根據base package在 該包中掃瞄所有類,查詢這些類是否有...

Spring控制Bean載入順序

spring容器載入bean順序是不確定的,spring框架沒有約定特定順序邏輯規範。首先要了解depends on或 dependson作用,是用來表示乙個bean a的例項化依賴另乙個bean b的例項化,但是a並不需要持有乙個b的物件,如果需要的話就不用depends on,直接用依賴注入就可...