@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文件中的屬性值。