侧边栏壁纸
  • 累计撰写 49 篇文章
  • 累计创建 23 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Spring Boot 读取配置的方式

阿砖
2024-03-29 / 0 评论 / 0 点赞 / 56 阅读 / 913 字
  1. 通过 @Value 读取

@Value("${property.name}")
private String propertyName;
  1. 使用 @ConfigurationProperties 将配置文件中的属性映射到一个对象

@ConfigurationProperties(prefix = "config")
public class ConfigProperties {
    private String propertyName;
}
  1. 通过 Environment 对象可以获取配置文件中的属性

@Autowired
private Environment env;

public String getProperty() {
    return env.getProperty("property.name");
}
  1. 使用 @PropertySource 注解

@Configuration
@PropertySource("classpath:custom.properties")
public class AppConfig {
}

0

评论区