這篇文章主要介紹了Spring注解@Value及屬性加載配置文件方式的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
spring中配置屬性加載文件的配置方式
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:/properties/websit.properties</value> </list> </property> </bean>
注意
1.這里使用的configProperties必須要和定義的bean名稱一致。
2.websit用來指定msgname來源于那個配置文件
3.配置的加載屬性bean名稱為org.springframework.beans.factory.config.PropertiesFactoryBean
使用這種方式,又可以有兩種配置方式
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties"/> </bean> <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:/properties/websit.properties</value> </list> </property> </bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:properties/websit.properties</value> </list> </property> </bean>
當使用@Value注解bean屬性時,如果沒有在配置文件中配置,這時啟動spring就會拋出異常。@Value提供了一種默認值的設置方式,如果在屬性文件中沒有配置則可以使用默認值。
形式如下
@Value("${avg.age:22}") private int userAge;
如果使用@Value注解后,數據不能正常的被注入則需要在xml的配置文件中加入下列代碼
<context:annotation-config/>
為了簡化讀取properties文件中的配置值,spring支持@value注解的方式來獲取,這種方式大大簡化了項目配置,提高業務中的靈活性。
1)@Value("#{configProperties['key']}")
2)@Value("${key}")
ftp:
ftplp: 10.2.23.89
ftpPort: 21
ftpUser: uftp
ftpPwd: 12345678
ftpRemotePath: /home
說明:以上是配置文件中的信息,主要是一些賬號密碼等信息。
package com.dbright.dataprediction.entity; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:ftpconfig.yml") @ConfigurationProperties(prefix = "ftp") public class FtpProperties { @Value("${ftplp}") public String ftplp; @Value("${ftpPort}") public String ftpPort; @Value("${ftpUser}") public String ftpUser; @Value("${ftpPwd}") public String ftpPwd; @Value("${ftpRemotePath}") public String ftpRemotePath; public String getFtplp() { return ftplp; } public void setFtplp(String ftplp) { this.ftplp = ftplp; } public String getFtpPort() { return ftpPort; } public void setFtpPort(String ftpPort) { this.ftpPort = ftpPort; } public String getFtpUser() { return ftpUser; } public void setFtpUser(String ftpUser) { this.ftpUser = ftpUser; } public String getFtpPwd() { return ftpPwd; } public void setFtpPwd(String ftpPwd) { this.ftpPwd = ftpPwd; } public String getFtpRemotePath() { return ftpRemotePath; } public void setFtpRemotePath(String ftpRemotePath) { this.ftpRemotePath = ftpRemotePath; } }
說明:以上是使用@value注解來讀取yml配置文件的代碼示例
1)@component
—— 把普通pojo實例化到spring容器中,相當于配置文件中的`<bean id="" class=""/>`
2) @PropertySource("classpath:ftpconfig.yml")
—— 設置yml文件的路徑,方便掃描到。一般我們配置文件都是放在resources包下。所以我們只需要 classpath+所需要讀取的配置文件名稱。
3)@ConfigurationProperties(prefix = "ftp")
—— 這個不需要解釋太多,配置文件里面內容的前綴,我們讀取的是ftp下的信息。
4)@Value("${ftplp}")
—— 這是讀取我們所需的配置信息,美元符號+{字段名}即可制定
5)下面定義字符串來接收所讀取到的配置信息。
6)寫set和get方法,方便外部類調用。
可以看到,我們成功取到了我們想要的值。
把{}外的 $ 變成 # 號,然后里面指定配置文件的信息+字段而已。大同小異,我就不貼代碼上來了。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Spring注解@Value及屬性加載配置文件方式的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。