问题:在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

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

  1. @Value取application.properties文件中的值

    问题描述 在使用@Value取application.properties文件中的值时,取出的值为null 问题解决: 1. 需要在所在的类上加@Component,把普通pojo实例化到spring ...

  2. Spring Boot 学习系列(07)—properties文件读取

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 传统的properties读取方式 一般的,我们都可以自定义一个xxx.properties文件,然后在工程 ...

  3. Spring Boot项目实战之aliyunOss对象储存#文件上传接口实现(代码齐全)

    项目环境:   开发工具:IDEA(jdk1.8)   模块类型:Mavan项目   OSS版本:2.8.3   开发者文档链接:https://help.aliyun.com/document_de ...

  4. Spring Boot 2.x基础教程:多文件的上传

    昨天,我们介绍了如何在Spring Boot中实现文件的上传(博客地址:https://blog.didispace.com/spring-boot-learning-21-4-3/).有读者问:那么 ...

  5. 前后端分离 -- 深入浅出 Spring Boot + Vue + ElementUI 实现相册管理系统【文件上传 分页 】 文件上传也不过如此~

    前后端分离 – 深入浅出系列 Spring Boot + Vue + ElementUI 实现相册管理系统[文件上传 分页 ] 文件上传也不过如此~ 引言 Hello,我是Bug终结者,一名热爱后端J ...

  6. java byte 图片浏览器直接显示_以Spring Boot的方式显示图片或下载文件到浏览器的示例代码...

    以Java web的方式显示图片到浏览器以Java web的方式下载服务器文件到浏览器 以Spring Boot的方式显示图片或下载文件到浏览器 请求例子:http://localhost:8080/ ...

  7. spring配置文件中分别使用多个properties文件

    spring配置文件中分别使用多个properties文件 在使用spring时,有时候需要为了模块配置方便有时候需要针对不同的模块建立不同的applicationContext的配置文件,然后在对应 ...

  8. 【springboot】SpringBootTest 测试时, application.properties 文件的查找顺序

    前言 springboot 2.0.0.RELEASE maven 3.5.0 application.properties 文件的查找顺序 ${project.basedir} ├─src │ ├─ ...

  9. 解决idea中的application.properties文件没有变成叶子形状

    可能你是springboot项目 但是你的application.properties文件却没有变成绿色的小叶子,可能是idea的加载问题,解决方案如下 将你的application.properti ...

最新文章

  1. 杭电 1272 poj 1308 小希的迷宫
  2. JSP 调用java 常量 枚举
  3. 节能与新能源汽车技术路线图2.0_节能与新能源汽车技术路线图(2.0版)
  4. 二叉搜索树介绍及其接口说明
  5. java ajax html复选框,HTMLjavaSkcriptCSSjQueryajax(九)(示例代码)
  6. aop注解配置切点 spring_Spring通过自定义注解灵活配置AOP切点
  7. openresty + nginx-http-sysguard 调研使用
  8. Android应用开发提高篇(4)-----Socket编程(多线程、双向通信)
  9. WIFI 802.11协议传输速率
  10. 支付网关清算及退款流程
  11. STM32单片机全自动锂电池容量电量检测放电电流电池电压ACS712
  12. 野火stm32f407学习笔记----核心板USB转TTL下载
  13. 惊!STM32 蓝牙串口模块(H21/JDY-31) 竟如此简单!
  14. 给快播指一条生路:转型会员付费吧
  15. C语言内存分配-附图详解,代码区、常量区、栈区、堆区.......
  16. CSDN取消手机绑定
  17. Java垃圾收集学习笔记
  18. OSChina 周日乱弹 ——xslai1210生日快乐
  19. [授权发表]源码分析:静态分析 C 程序函数调用关系
  20. GitHub集成Circle CI(附 Circle CI 配置示例文件)

热门文章

  1. C++::探索对象模型
  2. Java高级面试题!java编程思想怎么学
  3. Kafka的精髓全写这本“限量笔记”里了
  4. HTML如何添加锚点,论程序员成长的正确姿势
  5. 知乎上已获千赞,全网独家首发!
  6. NDK学习笔记-使用现有so动态库
  7. HDOJ 2037:今年暑假不AC_大二写
  8. os.path 模块
  9. mysql -- 索引的使用
  10. Kibana linux下安装