Spring載入多個配置檔案

2021-08-29 10:30:50 字數 673 閱讀 8288

關於spring載入多個配置檔案的方式:

1.  在web.xml檔案中註冊:

org.springframework.web.context.contextloaderlistener  

在乙個配置檔案中的bean想要引用另乙個檔案中的bean,可以使用這樣拿到。注意是bean=, 而不是local = . 如果是使用local = .表示從當前配置檔案中尋找bean, 如果bean是在其他檔案中則用bean = 來找。

beandefinitionregistry  reg = new defaultlistablebeanfactory();

xmlbeandefinitionreader reader = new xmlbeandefinitionreader(reg);

reader.loadbeandefinitions(new  classpathresource("bean1.xml"));

reader.loadbeandefinitions(new  classpathresource("bean2.xml"));

beanfactory bf =  (beanfactory)reg;

3. struts與spring整合時,還可選擇struts外掛程式來整合spring配置檔案。

在struts的配置檔案struts-config-*.xml最後新增:

spring載入多個配置檔案

載入器目前有兩種選擇 contextloaderlistener和contextloaderservlet。這兩者在功能上完全等同,只是乙個是基於servlet2.3版本中新引入的listener介面實現,而另乙個基於servlet介面實現。開發中可根據目標web容器的實際情況進行選擇。配置非常簡單...

Spring 怎樣載入多個bean 配置檔案

2 web 應用啟動時載入多個配置檔案 參看5.12.3 節所述,通過contextloaderlistener 也可載入多個配置檔案,可利用 337輕量級j2ee 企業應用實戰一一 struts spring hibernate 整合開發 元素來指定多個配置檔案位置,其配置如下 contextco...

spring配置檔案載入

spring配置檔案的裝載在web.xml中配置 此引數用於後面的spring context loader contextconfiglocation org.springframework.web.context.contextloaderlistener 說明 contextconfigloc...