在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;}
}
  1. 当你有了一个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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390932.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

连接sqlexpress

sqlexpress在visualstudio安装时可选择安装。   数据源添加 localhost\sqlexpress window身份认证即可。转载于:https://www.cnblogs.com/zjxbetter/p/7767241.html

在Python中使用Seaborn和WordCloud可视化YouTube视频

I am an avid Youtube user and love watching videos on it in my free time. I decided to do some exploratory data analysis on the youtube videos streamed in the US. I found the dataset on the Kaggle on this link我是YouTube的狂热用户,喜欢在业余时间…

Win下更新pip出现OSError:[WinError17]与PerrmissionError:[WinError5]及解决

环境:Win7 64位,python3.6.0 我在准备用pip装东西的时候,在cmd里先更新了一下pip,大概是9.0.1更新到9.0. 尝试更新pip命令: pip install --upgrade pip 更新一半挂了 出现了 OSError:[WinError17] 与 PerrmissionError…

老生常谈:抽象工厂模式

在创建型模式中有一个模式是不得不学的,那就是抽象工厂模式(Abstract Factory),这是创建型模式中最为复杂,功能最强大的模式.它常与工厂方法组合来实现。平时我们在写一个组件的时候一般只针对一种语言,或者说是针对一个区域的人来实现。 例如:现有有一个新闻组件,在中国我们有…

ogc是一个非营利性组织_非营利组织的软件资源

ogc是一个非营利性组织Please note that freeCodeCamp is not partnered with, nor do we receive a referral fee from, any of the following providers. We simply want to help guide you toward a solution for your organization.请注意,freeCodeCamp不与以下…

数据结构入门最佳书籍_最佳数据科学书籍

数据结构入门最佳书籍Introduction介绍 I get asked a lot what resources I recommend for people who want to start their Data Science journey. This section enlists books I recommend you should read at least once in your life as a Data Scientist.我被很多人问到…

函数式编程概念

什么是函数式编程 简单地说,函数式编程通过使用函数,将值转换成抽象单元,接着用于构建软件系统。 面向对象VS函数式编程 面向对象编程 面向对象编程认为一切事物皆对象,将现实世界的事物抽象成对象,现实世界中的关系抽…

在Java里面怎么样在静态方法中调用getClass()?

问题:在Java里面怎么样在静态方法中调用getClass()? 我有一个类,它必须包含一些静态方法,在这些静态方法里面我需要像下面那样调用getClass() 方法 public static void startMusic() {URL songPath getClass().getClassLoader(…

变量名和变量地址

变量名和变量地址 研一时,很偶然的翻开谭浩强老先生的《C程序设计》(是师姐的书,俺的老早就卖了,估计当时觉得这本书写得不够好),很偶然的看到关于变量名的一段话:“变量名实际上是一个符号地址…

多重插补 均值插补_Feature Engineering Part-1均值/中位数插补。

多重插补 均值插补Understanding the Mean /Median Imputation and Implementation using feature-engine….!了解使用特征引擎的均值/中位数插补和实现…。! 均值或中位数插补: (Mean or Median Imputation:) The mean or median value should be calc…

域 嵌入图像显示不出来_如何(以及为什么)将域概念嵌入代码中

域 嵌入图像显示不出来Code should clearly reflect the problem it’s solving, and thus openly expose that problem’s domain. Embedding domain concepts in code requires thought and skill, and doesnt drop out automatically from TDD. However, it is a necessary …

linux 查看用户上次修改密码的日期

查看root用户密码上次修改的时间 方法一:查看日志文件: # cat /var/log/secure |grep password changed 方法二: # chage -l root-----Last password change : Feb 27, 2018 Password expires : never…

spring里面 @Controller和@RestController注解的区别

问题:spring里面 Controller和RestController注解的区别 spring里面 Controller和RestController注解的区别 Web MVC和REST applications都可以用Controller吗? 如果是的话,怎么样区别这个一个 Web MVC还是REST application呢 回答一 下面…

2流程控制

分支、循环 str1$1 str2$2 echo $# if [ $str1 $str2 ] thenecho "ab" elif [ "$str1" -lt "$str2" ] thenecho "a < b" elif [ "$str1" -gt "$str2" ] thenecho "a > b" elseecho "没有符…

客户行为模型 r语言建模_客户行为建模:汇总统计的问题

客户行为模型 r语言建模As a Data Scientist, I spend quite a bit of time thinking about Customer Lifetime Value (CLV) and how to model it. A strong CLV model is really a strong customer behavior model — the better you can predict next actions, the better yo…

linux bash命令_Ultimate Linux命令行指南-Full Bash教程

linux bash命令Welcome to our ultimate guide to the Linux Command Line. This tutorial will show you some of the key Linux command line technologies and introduce you to the Bash scripting language.欢迎使用我们的Linux命令行最终指南。 本教程将向您展示一些关键…

【知识科普】解读闪电/雷电网络,零基础秒懂!

知识科普&#xff0c;解读闪电/雷电网络&#xff0c;零基础秒懂&#xff01; 闪电网络的技术是革命性的&#xff0c;将实现即时0手续费的小金额支付。第一步是解决扩容问题&#xff0c;第二部就是解决共通性问题&#xff0c;利用原子交换协议和不同链条的状态通道结合&#xff…

spring框架里面applicationContext.xml 和spring-servlet.xml 的区别

问题&#xff1a;spring框架里面applicationContext.xml 和spring-servlet.xml 的区别 在Spring框架中applicationContext.xml和Spring -servlet.xml有任何关系吗? DispatcherServlet可以使用到在applicationContext.xml中声明的属性文件吗? 另外&#xff0c;为什么我需要*…

Alpha 冲刺 (5/10)

【Alpha go】Day 5&#xff01; Part 0 简要目录 Part 1 项目燃尽图Part 2 项目进展Part 3 站立式会议照片Part 4 Scrum 摘要Part 5 今日贡献Part 1 项目燃尽图 Part 2 项目进展 已分配任务进度博客检索功能&#xff1a;根据标签检索流程图 -> 实现 -> 测试近期比…

多维空间可视化_使用GeoPandas进行空间可视化

多维空间可视化Recently, I was working on a project where I was trying to build a model that could predict housing prices in King County, Washington — the area that surrounds Seattle. After looking at the features, I wanted a way to determine the houses’ …