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名稱一致。
2.websit用來指定msgname來源于那個配置文件
3.配置的加載屬性bean名稱為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>
當使用@Value注解bean屬性時,如果沒有在配置文件中配置,這時啟動spring就會拋出異常。@Value提供了一種默認值的設(shè)置方式,如果在屬性文件中沒有配置則可以使用默認值。
形式如下
@Value("${avg.age:22}")
private int userAge;
如果使用@Value注解后,數(shù)據(jù)不能正常的被注入則需要在xml的配置文件中加入下列代碼
<context:annotation-config/>
SpringBoot使用注解(@value)讀取properties(yml)文件中 配置信息
為了簡化讀取properties文件中的配置值,spring支持@value注解的方式來獲取,這種方式大大簡化了項目配置,提高業(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
說明:以上是配置文件中的信息,主要是一些賬號密碼等信息。
3. 讀取yml配置文件的工具類
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") —— 設(shè)置yml文件的路徑,方便掃描到。一般我們配置文件都是放在resources包下。所以我們只需要 classpath+所需要讀取的配置文件名稱。
3)@ConfigurationProperties(prefix = "ftp") —— 這個不需要解釋太多,配置文件里面內(nèi)容的前綴,我們讀取的是ftp下的信息。
4)@Value("${ftplp}") —— 這是讀取我們所需的配置信息,美元符號+{字段名}即可制定
5)下面定義字符串來接收所讀取到的配置信息。
6)寫set和get方法,方便外部類調(diào)用。
4. 演示:效果圖如下


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

