@Value("${property.key}")
private String propertyValue;
@ConfigurationProperties(prefix = "property")
public class MyProperties {
private String key;
// getters and setters
}
@Autowired
private Environment env;
public void getProperty() {
String propertyValue = env.getProperty("property.key");
}
@PropertySource("classpath:config.properties")
@Configuration
public class AppConfig {
@Autowired
private Environment env;
@Value("${property.key}")
private String propertyValue;
}
这些技巧可以帮助SpringBoot应用程序方便地读取properties文件中的属性值。