国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Spring Boot讀取yml或者properties配置信息

這篇具有很好參考價(jià)值的文章主要介紹了Spring Boot讀取yml或者properties配置信息。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

Spring Boot讀取yml或者properties配置信息

方法一:@Value獲取基本信息,適用于少量信息

package com.geekmice.springbootselfexercise;

import com.geekmice.springbootselfexercise.config.DataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;

@Slf4j
@SpringBootTest(classes = SpringBootSelfExerciseApplication.class)
@RunWith(SpringRunner.class)
class SpringBootSelfExerciseApplicationTests {

    @Value("${server.port}")
    private String port;

    @Test
    void contextLoads() {
        log.info("端口號(hào):【{}】",port);
   }

}

Spring Boot讀取yml或者properties配置信息,問題匯總,spring boot,數(shù)據(jù)庫(kù),后端

方法二:通過注解@ConfigurationProperties(prefix = “spring.datasource”)

編寫配置類

package com.geekmice.springbootselfexercise.config;

import com.sun.media.jfxmedia.logging.Logger;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: spring-boot-self-exercise
 * @BelongsPackage: com.geekmice.springbootselfexercise.config
 * @Author: pingmingbo
 * @CreateTime: 2023-08-05  23:12
 * @Description: TODO
 * @Version: 1.0
 */

@ConfigurationProperties(prefix = "spring.datasource")
@Component
@Data
public class DataSourceProperties {
    private String username;
    private String password;
    private String url;
    private String driverClassName;

}

開始使用

package com.geekmice.springbootselfexercise;

import com.geekmice.springbootselfexercise.config.DataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;

@Slf4j
@SpringBootTest(classes = SpringBootSelfExerciseApplication.class)
@RunWith(SpringRunner.class)
class SpringBootSelfExerciseApplicationTests {

    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Test
    void contextLoads() {
        String username = dataSourceProperties.getUsername();
        String password = dataSourceProperties.getPassword();
        String url = dataSourceProperties.getUrl();
        String driverClassName = dataSourceProperties.getDriverClassName();
        log.info("用戶名:【{}】",username);
        log.info("密碼:【{}】",password);
        log.info("地址URL:【{}】",url);
        log.info("驅(qū)動(dòng)類:【{}】",driverClassName);
    }
}

Spring Boot讀取yml或者properties配置信息,問題匯總,spring boot,數(shù)據(jù)庫(kù),后端

方法三:通過api Environment

package com.geekmice.springbootselfexercise;

import com.geekmice.springbootselfexercise.config.DataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;

@Slf4j
@SpringBootTest(classes = SpringBootSelfExerciseApplication.class)
@RunWith(SpringRunner.class)
class SpringBootSelfExerciseApplicationTests {

    @Autowired
    private Environment environment;

    @Test
    public void t1(){
        String username = environment.getProperty("spring.datasource.username");
        String password = environment.getProperty("spring.datasource.password");
        String url = environment.getProperty("spring.datasource.url");
        String driverClassName = environment.getProperty("spring.datasource.driver-class-name");
        log.info("用戶名:【{}】",username);
        log.info("密碼:【{}】",password);
        log.info("地址URL:【{}】",url);
        log.info("驅(qū)動(dòng)類:【{}】",driverClassName);
    }
}

Spring Boot讀取yml或者properties配置信息,問題匯總,spring boot,數(shù)據(jù)庫(kù),后端文章來源地址http://www.zghlxwxcb.cn/news/detail-629606.html

到了這里,關(guān)于Spring Boot讀取yml或者properties配置信息的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • java-讀取配置文件自定義字段(yml、properties)

    java-讀取配置文件自定義字段(yml、properties)

    在springboot項(xiàng)目配置文件中設(shè)置自定義字段,項(xiàng)目代碼按需讀取,想換重要參數(shù)時(shí)直接更改配置文件即可,這篇文章說一說配置文件自定義字段的方法。 方法1 @Value 使用org.springframework.beans.factory.annotation包下的@Value注解讀取yml文件里面的字段,代碼如下: yml文件 讀取 測(cè)試結(jié)果

    2024年02月08日
    瀏覽(23)
  • 【SpringBoot】?jī)煞N配置文件, 詳解 properties 和 yml 的語法格式, 使用方式, 讀取配置

    【SpringBoot】?jī)煞N配置文件, 詳解 properties 和 yml 的語法格式, 使用方式, 讀取配置

    各位讀者好, 我是小陳, 這是我的個(gè)人主頁(yè), 希望我的專欄能夠幫助到你: ?? JavaSE基礎(chǔ): 基礎(chǔ)語法, 類和對(duì)象, 封裝繼承多態(tài), 接口, 綜合小練習(xí)圖書管理系統(tǒng)等 ?? Java數(shù)據(jù)結(jié)構(gòu): 順序表, 鏈表, 堆, 二叉樹, 二叉搜索樹, 哈希表等 ?? JavaEE初階: 多線程, 網(wǎng)絡(luò)編程, TCP/IP協(xié)議, HTTP協(xié)議

    2024年02月10日
    瀏覽(23)
  • SpringBoot中的yml文件中讀取自定義配置信息

    SpringBoot中的yml文件中讀取自定義配置信息

    開發(fā)中遇到的問題,百度的答案我都沒有找到,去找大佬獲取到的經(jīng)驗(yàn)總結(jié),這只是其中的一種方法,如果其他大佬有新的方法,可以分享分享。 1.1 yml文件 自定義配置信息,通過我們編寫的代碼讀取。 1.2 類 非靜態(tài)屬性中@Value生效 ps: 不是在控制層中擁有@Controller注解,說明

    2024年02月07日
    瀏覽(24)
  • SpringBoot支持bootstrap.yml/bootstrap.properties配置文件,需要導(dǎo)入spring-cloud-starter-bootstrap依賴

    說明: 在SpringBoot 2.4.x版本之后,對(duì)于bootstrap.properties/bootstrap.yaml配置文件的支持,需要導(dǎo)入如下依賴: 注意: 1、導(dǎo)入依賴后,還需要開啟?spring.cloud.bootstrap.enabled=true 2、spring cloud starter alibaba nacos config,引用這個(gè)配置中心的依賴后,需要使用bootstrap.yml或bootstrap.properties?作

    2024年02月13日
    瀏覽(21)
  • Java實(shí)戰(zhàn):Spring Boot application.yml配置文件詳解

    本文將詳細(xì)介紹Spring Boot application.yml 配置文件的使用和配置項(xiàng)。我們將探討 application.yml 文件的基本概念,以及如何使用它來配置Spring Boot應(yīng)用程序的各個(gè)方面。此外,我們將通過具體的示例來展示如何配置不同的Spring Boot組件,如數(shù)據(jù)源、數(shù)據(jù)庫(kù)、緩存、郵件服務(wù)等。本文適

    2024年04月24日
    瀏覽(24)
  • 在Spring Boot微服務(wù)使用jasypt-spring-boot加密和解密yml配置文件

    記錄 :424 場(chǎng)景 :在Spring Boot微服務(wù),使用jasypt-spring-boot加密和解密yml配置文件中的配置信息。 版本 :JDK 1.8,Spring?Boot 2.6.3,jasypt-1.9.3,jasypt-spring-boot-2.1.2, jasypt-spring-boot-3.0.5。 開源地址 :https://github.com/ulisesbocchio/jasypt-spring-boot 1.在Spring Boot微服務(wù)使用jasypt-spring-boot-3.0.5版本

    2024年02月09日
    瀏覽(21)
  • 聊聊Spring Boot配置文件:優(yōu)先級(jí)順序、加載順序、bootstrap.yml與application.yml區(qū)別詳解

    在 Spring Boot 中,配置文件的優(yōu)先級(jí)順序是: application-{profile}.yml ( application-{profile}.properties ) application.yml ( application.properties ) bootstrap.yml ( bootstrap.properties )。其中, {profile} 表示不同的環(huán)境配置,如 dev 、 test 、 prod 等。 優(yōu)先級(jí)從高到低,高優(yōu)先級(jí)的配置覆蓋低優(yōu)先級(jí)

    2024年01月25日
    瀏覽(31)
  • Spring Boot微服務(wù)從yml文件中加載配置(使用@Value和@ConfigurationProperties)

    記錄 :398 場(chǎng)景 :在Spring Boot的微服務(wù)中從application.yml等yml文件中加載自定義配置內(nèi)容。使用@Value直接加載單個(gè)配置。使用@ConfigurationProperties注解把一個(gè)或者多個(gè)配置加載為Java對(duì)象。 版本 :JDK 1.8,SpringBoot 2.6.3 1.使用@Value注解加載配置 使用注解@RestController、@Service、@Component等

    2024年02月12日
    瀏覽(21)
  • Spring Boot Application.properties和yaml配置文件

    Spring Boot Application.properties和yaml配置文件

    全局配置文件能夠?qū)σ恍┠J(rèn)配置值進(jìn)行修改。Spring Boot使用一個(gè)application.properties或者application.yaml的文件作為全局配置文件,該文件存放在src/main/resource目錄或者類路徑的/config,一般會(huì)選擇resource目錄。 使用Spring Initializr方式創(chuàng)建項(xiàng)目——PropertiesDemo 單擊【Next】按鈕 添加W

    2024年01月24日
    瀏覽(25)
  • 社區(qū)版Intellij IDEA安裝Spring Boot Assistant插件解決yml無提示問題

    社區(qū)版Intellij IDEA安裝Spring Boot Assistant插件解決yml無提示問題

    ? ? 如題所示,我們?nèi)绻麄€(gè)人使用免費(fèi)社區(qū)版的IDEA,它缺失了很多功能,使用起來沒有專業(yè)版那么強(qiáng)大,比如無法直接創(chuàng)建springbootinit項(xiàng)目,spring配置yml文件沒有提示。但是可以通過安裝一些插件,比如spring boot helper 、spring boot assistant插件來分別解決這兩個(gè)問題。 ? ? 下面

    2023年04月18日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包