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問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
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í)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
Logback在SpringBoot中的詳細(xì)配置教程
Spring Boot 默認(rèn)會加載 classpath 下的 logback-spring.xml(推薦)或 logback.xml 作為 Logback 的配置文件,下面我們來看看具體的配置教程吧2025-05-05
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
文章介紹了如何使用SpringA和Skills構(gòu)建一個代碼評審應(yīng)用,通過實(shí)戰(zhàn)示例展示如何利用SpringAI和大模型實(shí)現(xiàn)零配置的代碼評審功能,感興趣的朋友跟隨小編一起看看吧2026-04-04
SpringBoot集成Tess4J實(shí)現(xiàn)OCR的示例代碼
Tess4J是一個基于Tesseract OCR引擎的Java接口,可以用來識別圖像中的文本,說白了,就是封裝了它的API,讓Java可以直接調(diào)用,本文給大家介紹了SpringBoot集成Tess4J實(shí)現(xiàn)OCR的示例,需要的朋友可以參考下2024-12-12

