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;
}注冊(cè)屬性配置類
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 屬性注解說明是需要在啟動(dòng)類 Application 啟動(dòng)的時(shí)候加載。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java?web實(shí)現(xiàn)簡單注冊(cè)功能
這篇文章主要為大家詳細(xì)介紹了Java?web實(shí)現(xiàn)簡單注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
BeanUtils.copyProperties使用總結(jié)以及注意事項(xiàng)說明
這篇文章主要介紹了BeanUtils.copyProperties使用總結(jié)以及注意事項(xiàng)說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot + 微信公眾號(hào)JSAPI支付功能的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot + 微信公眾號(hào)JSAPI支付功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
SpringBoot的application.yml不生效問題及解決
這篇文章主要介紹了SpringBoot的application.yml不生效問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java線程中的用戶態(tài)和內(nèi)核態(tài)解讀
這篇文章主要介紹了Java線程中的用戶態(tài)和內(nèi)核態(tài)解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Java讀取properties文件內(nèi)容的幾種方式詳解
這篇文章主要介紹了Java讀取properties文件內(nèi)容的幾種方式詳解,讀取properties配置文件在實(shí)際的開發(fā)中使用的很多,本文來介紹常用的幾種實(shí)現(xiàn)方式,需要的朋友可以參考下2023-11-11
Java利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏
這篇文章主要介紹了利用Jackson序列化實(shí)現(xiàn)數(shù)據(jù)脫敏,首先在需要進(jìn)行脫敏的VO字段上面標(biāo)注相關(guān)脫敏注解,具體實(shí)例代碼文中給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10
Springboot整合Netty實(shí)現(xiàn)RPC服務(wù)器的示例代碼
這篇文章主要介紹了Springboot整合Netty實(shí)現(xiàn)RPC服務(wù)器的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

