我想访问应用程序中提供的值。属性,例如:
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应用程序的主程序中的路径。
当前回答
@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.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"));
我也有这个问题。但是有一个很简单的解决办法。只需要在构造函数中声明你的变量。
我的例子:
application.propperties:
#Session
session.timeout=15
SessionServiceImpl经济舱:
private final int SESSION_TIMEOUT;
private final SessionRepository sessionRepository;
@Autowired
public SessionServiceImpl(@Value("${session.timeout}") int sessionTimeout,
SessionRepository sessionRepository) {
this.SESSION_TIMEOUT = sessionTimeout;
this.sessionRepository = sessionRepository;
}
为了从属性文件中选择值,我们可以有一个配置读取器类,比如ApplicationConfigReader.java 然后根据属性定义所有变量。参考下面的例子,
application.properties
myapp.nationality: INDIAN
myapp.gender: Male
下面是相应的阅读类。
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "myapp")
class AppConfigReader{
private String nationality;
private String gender
//getter & setter
}
现在,我们可以在任何想要访问属性值的地方自动连接阅读器类。 如。
@Service
class ServiceImpl{
@Autowired
private AppConfigReader appConfigReader;
//...
//fetching values from config reader
String nationality = appConfigReader.getNationality() ;
String gender = appConfigReader.getGender();
}
你也可以这样做....
@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");
您可以使用@Value从应用程序加载变量。如果你在一个地方使用这个值,那么@ConfigurationProperties是一个更好的方法,但是如果你需要一个更集中的方式来加载这些变量。
此外,如果需要不同的数据类型来执行验证和业务逻辑,则可以加载变量并自动转换它们。
application.properties
custom-app.enable-mocks = false
@Value("${custom-app.enable-mocks}")
private boolean enableMocks;