最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot如何讀取application.properties配置文件

 更新時間:2024年05月09日 11:29:33   作者:培根芝士  
這篇文章主要介紹了SpringBoot如何讀取application.properties配置文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

方案1

使用PropertiesLoaderUtils

import java.util.Properties;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
 
public static Properties readPropertiesFile(String fileName) {
    try {
        Resource resource = new ClassPathResource(fileName);
        Properties props = PropertiesLoaderUtils.loadProperties(resource);
        return props;
    } catch (Exception e) {
        System.out.println("讀取配置文件:" + fileName + "異常,讀取失敗");
        e.printStackTrace();
    }
    return null;
}
Properties properties = readPropertiesFile("application.properties");
System.out.println(properties.getProperty("spring.rabbitmq.host"));

方案2

使用Environment

import org.springframework.core.env.Environment;
 
@Autowired
private Environment environment;
 
System.out.println(environment.getProperty("spring.rabbitmq.host"));

方案3

使用@Value

import org.springframework.beans.factory.annotation.Value;
 
@Value("${spring.rabbitmq.host}")
private String rabbitmqHost;
 
System.out.println(rabbitmqHost);

方案4

使用@ConfigurationProperties

屬性類

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
 
@Getter
@Setter
@ConfigurationProperties(prefix = "spring.rabbitmq")
public class TestProperties {
    private String host;
    private String port;
    private String username;
    private String password;
}

注冊屬性配置類

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
 
@EnableConfigurationProperties(TestProperties.class)
@SpringBootConfiguration
public class TestConfiguration {
}

使用配置類

@Autowired
private TestProperties testProperties;
 
System.out.println(testProperties.getHost());

static靜態(tài)方法讀取配置

@Component
public class UserUtil {
    // 使用@Value注解讀取配置
    @Value("${user.name}")
    private String name;
 
    // 設(shè)置靜態(tài)成員變量用來接收@Value注入的值
    private static String userName;
 
    // 使用@PostConstruct注解用于靜態(tài)變量賦值
    @PostConstruct
    public void getUserName() {
        userName = this.name;
    }
 
    // 測試方法靜態(tài)變量是否被賦值
    public static String test() {
        return userName;
    }
}

調(diào)用示例: 

String name = UserUtil.test();

使用 @Component 屬性注解說明是需要在啟動類 Application 啟動的時候加載。

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java?Socket實(shí)現(xiàn)文件發(fā)送和接收功能以及遇到的Bug問題

    Java?Socket實(shí)現(xiàn)文件發(fā)送和接收功能以及遇到的Bug問題

    這篇文章主要介紹了Java?Socket實(shí)現(xiàn)文件發(fā)送和接收功能以及遇到的Bug問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Spring Security基于JWT實(shí)現(xiàn)SSO單點(diǎn)登錄詳解

    Spring Security基于JWT實(shí)現(xiàn)SSO單點(diǎn)登錄詳解

    這篇文章主要介紹了Spring Security基于JWT實(shí)現(xiàn)SSO單點(diǎn)登錄詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Spring security密碼加密實(shí)現(xiàn)代碼實(shí)例

    Spring security密碼加密實(shí)現(xiàn)代碼實(shí)例

    這篇文章主要介紹了Spring security密碼加密實(shí)現(xiàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • 詳解在Spring Boot中使用Mysql和JPA

    詳解在Spring Boot中使用Mysql和JPA

    本文向你展示如何在Spring Boot的Web應(yīng)用中使用Mysq數(shù)據(jù)庫,也充分展示Spring Boot的優(yōu)勢
    2017-04-04
  • Logback在SpringBoot中的詳細(xì)配置教程

    Logback在SpringBoot中的詳細(xì)配置教程

    Spring Boot 默認(rèn)會加載 classpath 下的 logback-spring.xml(推薦)或 logback.xml 作為 Logback 的配置文件,下面我們來看看具體的配置教程吧
    2025-05-05
  • Java中將接口返回的字節(jié)串轉(zhuǎn)為文件詳解

    Java中將接口返回的字節(jié)串轉(zhuǎn)為文件詳解

    這篇文章主要給大家介紹了關(guān)于Java中將接口返回的字節(jié)串轉(zhuǎn)為文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-11-11
  • SpringAI?Agent開發(fā)秘籍如何讓javaer也可以用上Agent?Skills

    SpringAI?Agent開發(fā)秘籍如何讓javaer也可以用上Agent?Skills

    文章介紹了如何使用SpringA和Skills構(gòu)建一個代碼評審應(yīng)用,通過實(shí)戰(zhàn)示例展示如何利用SpringAI和大模型實(shí)現(xiàn)零配置的代碼評審功能,感興趣的朋友跟隨小編一起看看吧
    2026-04-04
  • SpringBoot集成Tess4J實(shí)現(xiàn)OCR的示例代碼

    SpringBoot集成Tess4J實(shí)現(xiàn)OCR的示例代碼

    Tess4J是一個基于Tesseract OCR引擎的Java接口,可以用來識別圖像中的文本,說白了,就是封裝了它的API,讓Java可以直接調(diào)用,本文給大家介紹了SpringBoot集成Tess4J實(shí)現(xiàn)OCR的示例,需要的朋友可以參考下
    2024-12-12
  • idea如何開啟菜單欄

    idea如何開啟菜單欄

    文章介紹了如何通過修改IntelliJ IDEA的樣式文件`ui.lnf.xml`來重新顯示被關(guān)閉的菜單欄,并分享了解決問題的步驟
    2025-01-01
  • 使用Logback設(shè)置日志級別

    使用Logback設(shè)置日志級別

    這篇文章主要介紹了使用Logback設(shè)置日志級別的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評論

左权县| 吕梁市| 河北省| 浪卡子县| 兴业县| 苏尼特右旗| 车险| 南京市| 潢川县| 高密市| 宁远县| 宣威市| 兴义市| 玉溪市| 西峡县| 三明市| 黎城县| 陇南市| 香港 | 太原市| 保定市| 镇巴县| 阿尔山市| 阜康市| 稻城县| 常山县| 特克斯县| 商都县| 盐津县| 泸溪县| 新乡市| 临沂市| 元江| 云浮市| 巴林左旗| 电白县| 白山市| 赤水市| 万全县| 怀化市| 乐都县|