亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2021年07月06日 11:18:37   作者:夢(mèng)幻家鄉(xiāng)16  
這篇文章主要介紹了Spring注解@Value及屬性加載配置文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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用來(lái)指定msgname來(lái)源于那個(gè)配置文件

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>

當(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配置文件的工具類

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+所需要讀取的配置文件名稱。

3)@ConfigurationProperties(prefix = "ftp") —— 這個(gè)不需要解釋太多,配置文件里面內(nèi)容的前綴,我們讀取的是ftp下的信息。

4)@Value("${ftplp}") —— 這是讀取我們所需的配置信息,美元符號(hào)+{字段名}即可制定

5)下面定義字符串來(lái)接收所讀取到的配置信息。

6)寫set和get方法,方便外部類調(diào)用。

4. 演示:效果圖如下

可以看到,我們成功取到了我們想要的值。

5. 一開(kāi)始說(shuō)的第二種和這個(gè)差不多

把{}外的 $ 變成 # 號(hào),然后里面指定配置文件的信息+字段而已。大同小異,我就不貼代碼上來(lái)了。

今天的內(nèi)容就到這里啦,以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家!

相關(guān)文章

最新評(píng)論