在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.log

userBucket.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 {

    @Autowired
    private 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
    @Bean
    public String getUserBucketPath() {
        return userBucket;
    }
}
  1. 当你有了一个configuration 类以后,就可以从configuration,注入你需要的变量了
@Component
public class YourService {

    @Autowired
    private 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值