我想访问应用程序中提供的值。属性,例如:

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

userBucket.path=${HOME}/bucket

我想访问userBucket。在Spring Boot应用程序的主程序中的路径。


当前回答

对我来说,以上这些方法对我都没有直接作用。 我所做的是:

另外,我补充了@Rodrigo Villalba Zayas的回答 实现InitializingBean到类 并实现了该方法

@Override
public void afterPropertiesSet() {
    String path = env.getProperty("userBucket.path");
}

这看起来就像

import org.springframework.core.env.Environment;
public class xyz implements InitializingBean {

    @Autowired
    private Environment env;
    private String path;

    ....

    @Override
    public void afterPropertiesSet() {
        path = env.getProperty("userBucket.path");
    }

    public void method() {
        System.out.println("Path: " + path);
    }
}

其他回答

您可以使用@Value从应用程序加载变量。如果你在一个地方使用这个值,那么@ConfigurationProperties是一个更好的方法,但是如果你需要一个更集中的方式来加载这些变量。

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

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

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

@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();

}

获取财产价值的最佳方法是使用。

1. 使用Value注释

@Value("${property.key}")
private String propertyKeyVariable;

2. 使用环境bean

@Autowired
private Environment env;

public String getValue() {
    return env.getProperty("property.key");
}

public void display(){
  System.out.println("# Value : "+getValue);
}

@Value Spring注释用于向Spring管理的bean中的字段注入值,它可以应用于字段或构造函数/方法参数级别。

例子

从注释到字段的字符串值

    @Value("string value identifire in property file")
    private String stringValue;

我们还可以使用@Value注释来注入Map属性。 首先,我们需要在属性文件中的{key: ' value'}形式中定义属性:

   valuesMap={key1: '1', key2: '2', key3: '3'}

并不是说Map中的值必须是单引号。

现在从属性文件中注入这个值作为Map:

   @Value("#{${valuesMap}}")
   private Map<String, Integer> valuesMap;

来获取特定键的值

   @Value("#{${valuesMap}.key1}")
   private Integer valuesMapKey1;

我们还可以使用@Value注释来注入List属性。

   @Value("#{'${listOfValues}'.split(',')}")
   private List<String> valuesList;

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

我们可以将每个环境的属性分离到单独的spring概要文件下的单独的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

从申请中读取。Yml或属性文件:

从属性文件或yml中读取值的最简单方法是使用spring @value注释。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