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

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;
 
    // 設置靜態(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;
    }
}

調用示例: 

String name = UserUtil.test();

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

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Java?web實現(xiàn)簡單注冊功能

    Java?web實現(xiàn)簡單注冊功能

    這篇文章主要為大家詳細介紹了Java?web實現(xiàn)簡單注冊功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • BeanUtils.copyProperties使用總結以及注意事項說明

    BeanUtils.copyProperties使用總結以及注意事項說明

    這篇文章主要介紹了BeanUtils.copyProperties使用總結以及注意事項說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn)

    SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn)

    這篇文章主要介紹了SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • SpringBoot的application.yml不生效問題及解決

    SpringBoot的application.yml不生效問題及解決

    這篇文章主要介紹了SpringBoot的application.yml不生效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java線程中的用戶態(tài)和內核態(tài)解讀

    Java線程中的用戶態(tài)和內核態(tài)解讀

    這篇文章主要介紹了Java線程中的用戶態(tài)和內核態(tài)解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Java讀取properties文件內容的幾種方式詳解

    Java讀取properties文件內容的幾種方式詳解

    這篇文章主要介紹了Java讀取properties文件內容的幾種方式詳解,讀取properties配置文件在實際的開發(fā)中使用的很多,本文來介紹常用的幾種實現(xiàn)方式,需要的朋友可以參考下
    2023-11-11
  • Spring Boot 3 整合 Spring Cloud Gateway實踐過程

    Spring Boot 3 整合 Spring Cloud 

    本文介紹了如何使用SpringCloudAlibaba2023.0.0.0版本構建一個微服務網(wǎng)關,包括統(tǒng)一路由、限流防刷和登錄鑒權等功能,并通過一個項目實例進行詳細說明,感興趣的朋友一起看看吧
    2025-02-02
  • Java提取兩個字符串中的相同元素方法

    Java提取兩個字符串中的相同元素方法

    今天小編就為大家分享一篇Java提取兩個字符串中的相同元素方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Java利用Jackson序列化實現(xiàn)數(shù)據(jù)脫敏

    Java利用Jackson序列化實現(xiàn)數(shù)據(jù)脫敏

    這篇文章主要介紹了利用Jackson序列化實現(xiàn)數(shù)據(jù)脫敏,首先在需要進行脫敏的VO字段上面標注相關脫敏注解,具體實例代碼文中給大家介紹的非常詳細,需要的朋友可以參考下
    2021-10-10
  • Springboot整合Netty實現(xiàn)RPC服務器的示例代碼

    Springboot整合Netty實現(xiàn)RPC服務器的示例代碼

    這篇文章主要介紹了Springboot整合Netty實現(xiàn)RPC服務器的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01

最新評論