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 啟動的時候加載。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
BeanUtils.copyProperties使用總結以及注意事項說明
這篇文章主要介紹了BeanUtils.copyProperties使用總結以及注意事項說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn)
這篇文章主要介紹了SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03SpringBoot的application.yml不生效問題及解決
這篇文章主要介紹了SpringBoot的application.yml不生效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Java利用Jackson序列化實現(xiàn)數(shù)據(jù)脫敏
這篇文章主要介紹了利用Jackson序列化實現(xiàn)數(shù)據(jù)脫敏,首先在需要進行脫敏的VO字段上面標注相關脫敏注解,具體實例代碼文中給大家介紹的非常詳細,需要的朋友可以參考下2021-10-10Springboot整合Netty實現(xiàn)RPC服務器的示例代碼
這篇文章主要介紹了Springboot整合Netty實現(xiàn)RPC服務器的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01