如何访问Spring Boot中application.properties文件中定义的值

问题描述 投票:211回答:6

我想访问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

java spring-boot properties-file
6个回答
333
投票

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

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

Spring Boot文档的Externalized Configuration部分解释了您可能需要的所有细节。


167
投票

另一种方法是向您的bean注入Environment。

@Autowired
private Environment env;
....

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

21
投票

@ConfigurationProperties可用于将.properties(也支持.yml)的值映射到POJO。

请考虑以下示例文件。

的.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
}

现在可以通过自动装配employeeProperties来访问属性值,如下所示。

@Autowired
private Employee employeeProperties;

public void method() {

   String employeeName = employeeProperties.getName();
   String employeeDept = employeeProperties.getDept();

}

3
投票

你也可以这样做....

@Component
@PropertySource("classpath:application.properties")
public class ConfigProperties {

    @Autowired
    private Environment env;

    public String getConfigValue(String configKey){
        return env.getProperty(configKey);
    }
}

然后,无论您想从application.properties中读取什么,只需将密钥传递给getConfigValue方法即可。

@Autowired
ConfigProperties configProp;

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

1
投票

你可以使用@Valueapplication.properties加载变量,如果你将在一个地方使用这个值,但如果你需要一个更集中的方式加载这个变量@ConfigurationProperties是一个更好的方法。

此外,如果需要不同的数据类型来执行验证和业务逻辑,您可以加载变量并自动转换它们。

application.properties
custom-app.enable-mocks = false

@Value("${custom-app.enable-mocks}")
private boolean enableMocks;

0
投票

Spring-boot允许我们提供几种方法来提供外部化配置,您可以尝试使用application.yml或yaml文件而不是属性文件,并根据不同的环境提供不同的属性文件设置。

我们可以将每个环境的属性分离到单独的弹簧配置文件下的单独的yml文件中。然后在部署期间,您可以使用:

java -jar -Drun.profiles=SpringProfileName

指定要使用的弹簧配置文件。请注意yml文件应该是名称

application-{environmentName}.yml

让它们被弹簧靴自动占用。

参考:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties

要从application.yml或属性文件中读取:

从属性文件或yml中读取值的最简单方法是使用spring @value annotation.Spring自动将yml中的所有值加载到spring环境中,这样我们就可以直接使用环境中的值,如:

@Component
public class MySampleBean {

@Value("${name}")
private String sampleName;

// ...

}

或者spring提供读取强类型bean的另一种方法如下:

YML

ymca:
    remote-address: 192.168.1.1
    security:
        username: admin

对应的POJO读取yml:

@ConfigurationProperties("ymca")
public class YmcaProperties {
    private InetAddress remoteAddress;
    private final Security security = new Security();
    public boolean isEnabled() { ... }
    public void setEnabled(boolean enabled) { ... }
    public InetAddress getRemoteAddress() { ... }
    public void setRemoteAddress(InetAddress remoteAddress) { ... }
    public Security getSecurity() { ... }
    public static class Security {
        private String username;
        private String password;
        public String getUsername() { ... }
        public void setUsername(String username) { ... }
        public String getPassword() { ... }
        public void setPassword(String password) { ... }
    }
}

上述方法适用于yml文件。

参考:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

© www.soinside.com 2019 - 2024. All rights reserved.