欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring注解@Value及屬性加載配置文件方式

 更新時間:2021年07月06日 11:18:37   作者:夢幻家鄉(xiāng)16  
這篇文章主要介紹了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)文章

最新評論