日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

在Spring Boot里面,怎么获取定义在application.properties文件里的值

發布時間:2023/11/29 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Spring Boot里面,怎么获取定义在application.properties文件里的值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題:在Spring Boot里面,怎么獲取定義在application.properties文件里的值、

我想訪問application.properties里面提供的值,像這樣:

logging.level.org.springframework.web: DEBUG logging.level.org.hibernate: ERROR logging.file=${HOME}/application.loguserBucket.path=${HOME}/bucket

我想要在spring boot的主程序里訪問userBucket.path

回答一

Another way is injecting org.springframework.core.env.Environment to your bean.
另一種方法就把org.springframework.core.env.Environment注入到你的bean里面

@Autowired private Environment env; ....public void method() {..... String path = env.getProperty("userBucket.path");..... }

回答二

@ConfigurationProperties可以用于將.properties( .yml也行)的值映射到一個POJO

Consider the following Example file.
看一下下面的實例文件

.properties

cust.data.employee.name=Sachin cust.data.employee.dept=Cricket

Employee.java

import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration;@ConfigurationProperties(prefix = "cust.data.employee") @Configuration("employeeProperties") public class Employee {private String name;private String dept;//Getters and Setters go here } Now the properties value can be accessed by autowiring employeeProperties as follows.

現在properties里面的值可以通過像下面那樣@Autowired一個employeeProperties,從而被訪問到。

@Autowired private Employee employeeProperties;public void method() {String employeeName = employeeProperties.getName();String employeeDept = employeeProperties.getDept();}

回答三

你也可以這樣做

@Component @PropertySource("classpath:application.properties") public class ConfigProperties {@Autowiredprivate Environment env;public String getConfigValue(String configKey){return env.getProperty(configKey);} }

無論你想怎么樣讀取application.properties,只要傳遞個key給getConfigValue方法就完事了

@Autowired ConfigProperties configProp;// Read server.port from app.prop String portNumber = configProp.getConfigValue("server.port");

回答四

follow these steps. 1:- create your configuration class like below you can see
按這些步驟干:1. 創建你的 configuration類,像你下面看到的那樣

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.beans.factory.annotation.Value;@Configuration public class YourConfiguration{// passing the key which you set in application.properties@Value("${userBucket.path}")private String userBucket;// getting the value from that key which you set in application.properties@Beanpublic String getUserBucketPath() {return userBucket;} }
  • 當你有了一個configuration 類以后,就可以從configuration,注入你需要的變量了
  • @Component public class YourService {@Autowiredprivate String getUserBucketPath;// now you have a value in getUserBucketPath varibale automatically. }

    文章翻譯自Stack Overflow:https://stackoverflow.com/questions/30528255/how-to-access-a-value-defined-in-the-application-properties-file-in-spring-boot

    總結

    以上是生活随笔為你收集整理的在Spring Boot里面,怎么获取定义在application.properties文件里的值的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。