Spring注解@Value及屬性加載配置文件方式
Spring中使用@Value注解給bean加載屬性的配置文件有兩種使用方式
第一種:使用@Value("#{configProperties['websit.msgname']}")
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名稱(chēng)一致。
2.websit用來(lái)指定msgname來(lái)源于那個(gè)配置文件
3.配置的加載屬性bean名稱(chēng)為org.springframework.beans.factory.config.PropertiesFactoryBean
第二種:使用@Value("${websit.msgname}");
使用這種方式,又可以有兩種配置方式
方式一
<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>
當(dāng)使用@Value注解bean屬性時(shí),如果沒(méi)有在配置文件中配置,這時(shí)啟動(dòng)spring就會(huì)拋出異常。@Value提供了一種默認(rèn)值的設(shè)置方式,如果在屬性文件中沒(méi)有配置則可以使用默認(rèn)值。
形式如下
@Value("${avg.age:22}")
private int userAge;
如果使用@Value注解后,數(shù)據(jù)不能正常的被注入則需要在xml的配置文件中加入下列代碼
<context:annotation-config/>
SpringBoot使用注解(@value)讀取properties(yml)文件中 配置信息
為了簡(jiǎn)化讀取properties文件中的配置值,spring支持@value注解的方式來(lái)獲取,這種方式大大簡(jiǎn)化了項(xiàng)目配置,提高業(yè)務(wù)中的靈活性。
1. 兩種使用方法
1)@Value("#{configProperties['key']}")
2)@Value("${key}")
2. 配置文件示例
ftp:
ftplp: 10.2.23.89
ftpPort: 21
ftpUser: uftp
ftpPwd: 12345678
ftpRemotePath: /home
說(shuō)明:以上是配置文件中的信息,主要是一些賬號(hào)密碼等信息。
3. 讀取yml配置文件的工具類(lèi)
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;
}
}
說(shuō)明:以上是使用@value注解來(lái)讀取yml配置文件的代碼示例
1)@component —— 把普通pojo實(shí)例化到spring容器中,相當(dāng)于配置文件中的`<bean id="" class=""/>`
2) @PropertySource("classpath:ftpconfig.yml") —— 設(shè)置yml文件的路徑,方便掃描到。一般我們配置文件都是放在resources包下。所以我們只需要 classpath+所需要讀取的配置文件名稱(chēng)。
3)@ConfigurationProperties(prefix = "ftp") —— 這個(gè)不需要解釋太多,配置文件里面內(nèi)容的前綴,我們讀取的是ftp下的信息。
4)@Value("${ftplp}") —— 這是讀取我們所需的配置信息,美元符號(hào)+{字段名}即可制定
5)下面定義字符串來(lái)接收所讀取到的配置信息。
6)寫(xiě)set和get方法,方便外部類(lèi)調(diào)用。
4. 演示:效果圖如下


可以看到,我們成功取到了我們想要的值。
5. 一開(kāi)始說(shuō)的第二種和這個(gè)差不多
把{}外的 $ 變成 # 號(hào),然后里面指定配置文件的信息+字段而已。大同小異,我就不貼代碼上來(lái)了。
今天的內(nèi)容就到這里啦,以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家!
相關(guān)文章
關(guān)于Selenium的UI自動(dòng)化測(cè)試屏幕截圖功能實(shí)例代碼
今天小編就為大家分享一篇關(guān)于Selenium的UI自動(dòng)化測(cè)試屏幕截圖功能實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Java concurrency線程池之線程池原理(二)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java concurrency線程池之線程池原理第二篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Java實(shí)現(xiàn)讀取163郵箱,qq郵箱的郵件內(nèi)容
這篇文章主要利用Java語(yǔ)言實(shí)現(xiàn)讀取163郵箱和qq郵箱的郵件內(nèi)容,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手試一試2022-02-02
詳解Spring Kafka中關(guān)于Kafka的配置參數(shù)
這篇文章主要介紹了詳解Spring Kafka中關(guān)于Kafka的配置參數(shù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例(附源碼)
這篇文章主要介紹了Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例, 相信使用過(guò)Spring的眾多開(kāi)發(fā)者都知道Spring提供了非常好用的JavaMailSender接口實(shí)現(xiàn)郵件發(fā)送。在Spring Boot的Starter模塊中也為此提供了自動(dòng)化配置。需要的朋友可以參考借鑒。2017-02-02
java: 錯(cuò)誤: 無(wú)效的源發(fā)行版18問(wèn)題及解決
這篇文章主要介紹了java: 錯(cuò)誤: 無(wú)效的源發(fā)行版18問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
SpringBoot實(shí)現(xiàn)指標(biāo)監(jiān)控
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)指標(biāo)監(jiān)控方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
springboot整合apache ftpserver詳細(xì)教程(推薦)
這篇文章主要介紹了springboot整合apache ftpserver詳細(xì)教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01

