我想访问应用程序中提供的值。属性,例如:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.file=${HOME}/application.log
userBucket.path=${HOME}/bucket
我想访问userBucket。在Spring Boot应用程序的主程序中的路径。
我想访问应用程序中提供的值。属性,例如:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.file=${HOME}/application.log
userBucket.path=${HOME}/bucket
我想访问userBucket。在Spring Boot应用程序的主程序中的路径。
当前回答
您可以使用@Value注释并访问spring bean中的属性
@Value("${userBucket.path}")
private String userBucketPath;
其他回答
对我来说,以上这些方法对我都没有直接作用。 我所做的是:
另外,我补充了@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);
}
}
@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:-创建你的配置类如下所示
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;
}
}
2:-当你有一个配置类时,然后从你需要的配置中注入变量。
@Component
public class YourService {
@Autowired
private String getUserBucketPath;
// now you have a value in getUserBucketPath varibale automatically.
}
1.Injecting a property with the @Value annotation is straightforward:
@Value( "${jdbc.url}" )
private String jdbcUrl;
2. we can obtain the value of a property using the Environment API
@Autowired
private Environment env;
...
dataSource.setUrl(env.getProperty("jdbc.url"));
@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;