spring掃瞄配置檔案

2022-07-07 13:54:16 字數 1436 閱讀 2996

spring配置檔案對應的是父容器,springmvc配置檔案產生的是子容器,前者一般配置資料來源,事務,註解等,當然還可以進一步將一些配置細化到其他xml中;後者一般配置控制層相關的,如靜態資源,視**析器等。系統啟動的時候,先初始化父容器,然後初始化子容器。這裡會涉及乙個問題,如果配置元件掃瞄時都配置全元件掃瞄,就會導致service元件會被掃瞄兩次,造成事務無法處理。所以最好在springmvc配置檔案中只做controller的掃瞄,在spring配置檔案中掃瞄其他元件。 

在spring的配置檔案中配置:

在springmvc的配置檔案中配置:

這樣就能各司其職了。 

在使用中,這兩個配置檔案作用不同。如果要使用@value注入一些系統配置檔案中的變數時要注意:如果要在controller中使用注入的變數,需要在springmvc的配置檔案中配置:

如果只在spring的配置檔案中配置,那麼在controller中是不會注入成功的。 

測試demo如下:

@runwith(springjunit4classrunner.class)

"classpath:servlet-dispatcher.xml"})

public class injectest ")

private string key;

@test

public void test()

基於@value進行注入時有兩種方式,佔位符和spel表示式

//佔位符方式

@value("$")

private string url;

//spel表達方式,其中代表xml配置檔案中的id值configproperties

@value("#")

private string username;

這兩種方式需要在xml中配置時也是不一樣的

classpath:config/resource/dev/lyframework.propertiesvalue>

classpath:config/resource/dev/common.propertiesvalue>

list>

property>

bean>

classpath:/conf/jdbc.propertiesvalue>

list>

property>

bean>

spring配置檔案

1 配置檔案的簡化 1 屬性的 property name foo value foovalue value property 簡化為 property name foo value foovalue 2 引用bean property name foo ref bean foobean prope...

spring配置檔案

context annotation config 是用於啟用那些已經在spring容器裡註冊過的bean上面的註解,也就是顯示的向spring註冊 autowiredannotationbeanpostprocessor commonannotationbeanpostprocessor pers...

Spring配置檔案載入外部配置檔案

有時,應用程式可能需要從不同的位置 例如 檔案系統 classpath或者url 讀取外部資源 例如 文字檔案 xml檔案 屬性檔案或或者影象檔案 通常,為了從不同位置載入資源,需要和不同的api打交道。spring的資源載入器提供了乙個統一的getresource 方法,使用這個方法可以通過資源路...