我想访问应用程序中提供的值。属性,例如:
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应用程序的主程序中的路径。
当前回答
你也可以这样做....
@Component
@PropertySource("classpath:application.properties")
public class ConfigProperties {
@Autowired
private Environment env;
public String getConfigValue(String configKey){
return env.getProperty(configKey);
}
}
然后无论你想从应用程序中读取什么。属性,只需将键传递给getConfigValue方法。
@Autowired
ConfigProperties configProp;
// Read server.port from app.prop
String portNumber = configProp.getConfigValue("server.port");
其他回答
你也可以这样做....
@Component
@PropertySource("classpath:application.properties")
public class ConfigProperties {
@Autowired
private Environment env;
public String getConfigValue(String configKey){
return env.getProperty(configKey);
}
}
然后无论你想从应用程序中读取什么。属性,只需将键传递给getConfigValue方法。
@Autowired
ConfigProperties configProp;
// Read server.port from app.prop
String portNumber = configProp.getConfigValue("server.port");
@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();
}
实际上有三种方法来读取应用程序。属性文件,
使用环境,
@Autowired
Environment environment
environment.getProperty({propertyName})
或者使用@Value,
@Value("${property}")
但是@Value的问题是,如果值不在属性文件中,它可能会抛出异常
建议使用@ConfigurationProperties
@ConfigurationProperties("userBucket")
public class test{
private String path;
//getters and setters
}
详细示例-读取application.properties
有两种方法,
你可以直接在类中使用@Value
@Value("#{'${application yml field name}'}")
public String ymlField;
OR
要使它干净,你可以清除@Configuration类,在那里你可以添加所有的@value
@Configuration
public class AppConfig {
@Value("#{'${application yml field name}'}")
public String ymlField;
}
遵循以下步骤。 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.
}