如何在 Spring Boot 中访问 application.properties 文件中定义的值 [英] How to access a value defined in the application.properties file in Spring Boot

查看:47
本文介绍了如何在 Spring Boot 中访问 application.properties 文件中定义的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问 application.properties 中提供的值,例如:

I want to access values provided in application.properties, e.g.:

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

userBucket.path=${HOME}/bucket

我想在 Spring Boot 应用程序的主程序中访问 userBucket.path.

I want to access userBucket.path in my main program in a Spring Boot application.

推荐答案

您可以使用 @Value 批注并访问您正在使用的任何 Spring bean 中的属性

You can use the @Value annotation and access the property in whichever Spring bean you're using

@Value("${userBucket.path}")
private String userBucketPath;

外部化配置部分Spring Boot 文档,解释了您可能需要的所有详细信息.

The Externalized Configuration section of the Spring Boot docs, explains all the details that you might need.

这篇关于如何在 Spring Boot 中访问 application.properties 文件中定义的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆