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

SpringBoot讀取properties配置文件中的數(shù)據(jù)的三種方法

 更新時(shí)間:2024年06月21日 10:43:33   作者:程序員null  
本文主要介紹了SpringBoot讀取properties配置文件中的數(shù)據(jù)的三種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Spring Boot最常用的3種讀取properties配置文件中數(shù)據(jù)的方法:

1、使用@Value注解讀取

讀取properties配置文件時(shí),默認(rèn)讀取的是application.properties。

application.properties:

demo.name=Name
demo.age=18

Java代碼:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GatewayController {

    @Value("${demo.name}")
    private String name;

    @Value("${demo.age}")
    private String age;

    @RequestMapping(value = "/gateway")
    public String gateway() {
        return "get properties value by ''@Value'' :" +
                //1、使用@Value注解讀取
                " name=" + name +
                " , age=" + age;
    }
}

運(yùn)行結(jié)果如下:

這里,如果要把

 @Value("${demo.name}")
            private String name;
            @Value("${demo.age}")
            private String age;

部分放到一個(gè)單獨(dú)的類A中進(jìn)行讀取,然后在類B中調(diào)用,則要把類A增加@Component注解,并在類B中使用@Autowired自動(dòng)裝配類A,代碼如下。

類A:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ConfigBeanValue {

    @Value("${demo.name}")
    public String name;

    @Value("${demo.age}")
    public String age;
}

類B:

import cn.wbnull.springbootdemo.config.ConfigBeanValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GatewayController {

    @Autowired
    private ConfigBeanValue configBeanValue;

    @RequestMapping(value = "/gateway")
    public String gateway() {
        return "get properties value by ''@Value'' :" +
                //1、使用@Value注解讀取
                " name=" + configBeanValue.name +
                " , age=" + configBeanValue.age;
    }
}

運(yùn)行結(jié)果如下:

?注意:如果@Value${}所包含的鍵名在application.properties配置文件中不存在的話,會拋出異常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configBeanValue': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'demo.name' in value "${demo.name}"

2、使用Environment讀取

application.properties:

demo.sex=男
demo.address=山東

Java代碼:

import cn.wbnull.springbootdemo.config.ConfigBeanValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GatewayController {

    @Autowired
    private ConfigBeanValue configBeanValue;

    @Autowired
    private Environment environment;

    @RequestMapping(value = "/gateway")
    public String gateway() {
        return "get properties value by ''@Value'' :" +
                //1、使用@Value注解讀取
                " name=" + configBeanValue.name +
                " , age=" + configBeanValue.age +
                "<p>get properties value by ''Environment'' :" +
                //2、使用Environment讀取
                " , sex=" + environment.getProperty("demo.sex") +
                " , address=" + environment.getProperty("demo.address");
    }
}

運(yùn)行,發(fā)現(xiàn)中文亂碼:

?這里,我們在application.properties做如下配置:

server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8

然后修改IntelliJ IDEA,F(xiàn)ile --> Settings --> Editor --> File Encodings ,將最下方Default encoding for properties files設(shè)置為UTF-8,并勾選Transparent native-to-ascii conversion。

?重新運(yùn)行結(jié)果如下:

3、使用@ConfigurationProperties注解讀取

在實(shí)際項(xiàng)目中,當(dāng)項(xiàng)目需要注入的變量值很多時(shí),上述所述的兩種方法工作量會變得比較大,這時(shí)候我們通常使用基于類型安全的配置方式,將properties屬性和一個(gè)Bean關(guān)聯(lián)在一起,即使用注解@ConfigurationProperties讀取配置文件數(shù)據(jù)。

在src\main\resources下新建config.properties配置文件:

demo.phone=10086
demo.wife=self

創(chuàng)建ConfigBeanProp并注入config.properties中的值:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "demo")
@PropertySource(value = "config.properties")
public class ConfigBeanProp {

    private String phone;

    private String wife;

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }
}

@Component 表示將該類標(biāo)識為Bean

@ConfigurationProperties(prefix = "demo")用于綁定屬性,其中prefix表示所綁定的屬性的前綴。

@PropertySource(value = "config.properties")表示配置文件路徑。

使用時(shí),先使用@Autowired自動(dòng)裝載ConfigBeanProp,然后再進(jìn)行取值,示例如下:

import cn.wbnull.springbootdemo.config.ConfigBeanProp;
import cn.wbnull.springbootdemo.config.ConfigBeanValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GatewayController {

    @Autowired
    private ConfigBeanValue configBeanValue;

    @Autowired
    private Environment environment;

    @Autowired
    private ConfigBeanProp configBeanProp;

    @RequestMapping(value = "/gateway")
    public String gateway() {
        return "get properties value by ''@Value'' :" +
                //1、使用@Value注解讀取
                " name=" + configBeanValue.name +
                " , age=" + configBeanValue.age +
                "<p>get properties value by ''Environment'' :" +
                //2、使用Environment讀取
                " sex=" + environment.getProperty("demo.sex") +
                " , address=" + environment.getProperty("demo.address") +
                "<p>get properties value by ''@ConfigurationProperties'' :" +
                //3、使用@ConfigurationProperties注解讀取
                " phone=" + configBeanProp.getPhone() +
                " , wife=" + configBeanProp.getWife();
    }
}

運(yùn)行結(jié)果如下:

到此這篇關(guān)于SpringBoot讀取properties配置文件中的數(shù)據(jù)的三種方法的文章就介紹到這了,更多相關(guān)SpringBoot properties讀取配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題

    詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題

    本文主要介紹了詳解Spring Cache使用Redisson分布式鎖解決緩存擊穿問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Maven依賴沖突原因以及解決方法

    Maven依賴沖突原因以及解決方法

    依賴沖突是指項(xiàng)目依賴的某一個(gè) jar 包,有多個(gè)不同的版本,因而造成類包版本沖突依賴沖突很經(jīng)常是類包之間的間接依賴引起的,本文將給大家介紹Maven依賴沖突原因以及解決方法,需要的朋友可以參考下
    2023-12-12
  • 淺談Java中方法參數(shù)傳遞的問題

    淺談Java中方法參數(shù)傳遞的問題

    下面小編就為大家?guī)硪黄獪\談Java中方法參數(shù)傳遞的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • MyBatis-Plus的apply用法小結(jié)

    MyBatis-Plus的apply用法小結(jié)

    apply方法是一個(gè)非常有用的功能,apply方法允許用戶直接在QueryWrapper或LambdaQueryWrapper中添加原生SQL片段,本文就詳細(xì)的介紹一下apply方法,感興趣的可以了解一下
    2024-10-10
  • Spring?BeanFactory工廠使用教程

    Spring?BeanFactory工廠使用教程

    Spring的本質(zhì)是一個(gè)bean工廠(beanFactory)或者說bean容器,它按照我們的要求,生產(chǎn)我們需要的各種各樣的bean,提供給我們使用。只是在生產(chǎn)bean的過程中,需要解決bean之間的依賴問題,才引入了依賴注入(DI)這種技術(shù)
    2023-02-02
  • springboot冪等切片的實(shí)現(xiàn)

    springboot冪等切片的實(shí)現(xiàn)

    本文主要介紹了springboot冪等切片的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 深度解析Java中的國際化底層類ResourceBundle

    深度解析Java中的國際化底層類ResourceBundle

    做項(xiàng)目應(yīng)該都會實(shí)現(xiàn)國際化,那么大家知道Java底層是如何實(shí)現(xiàn)國際化的嗎?這篇文章就來和大家深度解析一下Java中的國際化底層類ResourceBundle,希望對大家有所幫助
    2023-03-03
  • spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法

    spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法

    最近寫了關(guān)于圖片上傳至本地文件夾或服務(wù)器,上傳路徑到數(shù)據(jù)庫,并在上傳時(shí)預(yù)覽圖片。本文通過實(shí)例代碼給大家分享spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法,需要的朋友參考下
    2018-12-12
  • Java 不使用第三方變量交換兩個(gè)變量值的四種方法詳解

    Java 不使用第三方變量交換兩個(gè)變量值的四種方法詳解

    這篇文章主要介紹了四種不使用第三方變量交換兩個(gè)變量值的方法。文中對于四種方法進(jìn)行了詳細(xì)的分析,需要的小伙伴們可以跟隨小編一起學(xué)習(xí)一下
    2021-12-12
  • Spring Boot利用@Async異步調(diào)用:使用Future及定義超時(shí)詳解

    Spring Boot利用@Async異步調(diào)用:使用Future及定義超時(shí)詳解

    這篇文章主要給大家介紹了關(guān)于Spring Boot利用@Async異步調(diào)用:使用Future及定義超時(shí)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2018-05-05

最新評論