Spring中 Value用法收集

2022-03-15 22:57:31 字數 1619 閱讀 5946

一、配置方式

@value需要引數,這裡引數可以是兩種形式:

@value("#")

或者@value("$");

這兩形式,在配置上有什麼區別:

1、@value("#")這種形式的配置中有「configproperties」,其實它指定的是配置檔案的載入物件:配置如下:

<

bean

id="configproperties"

class

="org.springframework.beans.factory.config.propertiesfactorybean"

>

<

property

name

="locations"

>

<

list

>

<

value

>classpath:/config/t1.properties

value

>

list

>

property

>

bean

>

這樣配置就可完成對屬性的具體注入;

2、@value("$")這種形式不需要指定具體載入物件,這時候需要乙個關鍵的物件來完成preferencesplaceholderconfigurer,

這個物件的配置可以利用上面配置1中的配置,也可以自己直接自定配置檔案路徑。

如果使用配置1中的配置,可以寫成如下情況:

<

bean

id="propertyconfigurer"

class

="org.springframework.beans.factory.config.preferencesplaceholderconfigurer"

>

<

property

name

="properties"

ref="configproperties"

/>

bean

>

如果直接指定配置檔案的話,可以寫成如下情況: 

<

bean

id="propertyconfigurer"

class

="org.springframework.beans.factory.config.preferencesplaceholderconfigurer"

>

<

property

name

="location"

>

<

value

>config/t1.properties

value

>

property

>

bean

>

二、用法

spring 通過註解獲取*.porperties檔案的內容,除了xml配置外,還可以通過@value方式來獲取。

使用方式必須在當前類使用@component,xml檔案內配置的是通過pakage掃瞄方式,

例如:參考:

以上內容**此篇文章)

spring中的 Value的使用

1.首先這個value是只能乙個對應乙個 就是說,你不能設定list型別的,如果配置檔案中設定的是list型別的,它可能會獲取不到,可以使用別的註解,如果非得使用value這個註解那麼請看下邊解決辦法 目前我自己的解決辦法 在配置檔案中使用乙個固定的分隔符將值分隔開,然後bean中使用string型...

spring註解 value使用

public class bike public void setname string name value private string name public string getname configuration propertysource classpath test.properti...

spring中 value註解需要注意

首先,value需要引數,這裡引數可以是兩種形式 value 或者 value 其次,下面我們來看看如何使用這兩形式,在配置上有什麼區別 1 value 這種形式的配置中有 configproperties 其實它指定的是配置檔案的載入物件 配置如下 classpath config t1.prop...