通过
@Value
读取
@Value("${property.name}")
private String propertyName;
使用
@ConfigurationProperties
将配置文件中的属性映射到一个对象
@ConfigurationProperties(prefix = "config")
public class ConfigProperties {
private String propertyName;
}
通过
Environment
对象可以获取配置文件中的属性
@Autowired
private Environment env;
public String getProperty() {
return env.getProperty("property.name");
}
使用
@PropertySource
注解
@Configuration
@PropertySource("classpath:custom.properties")
public class AppConfig {
}
评论区