SpringBoot 获取配置文件

1. 配置文件内容

wechat:
  mpAppId: wxdf2b09f2812ef6e2
  mpAppSecret: f924b2e9f140acdjf559cb5317a8951c71
  
project:
  url:
    sell: http://localhost:8080  

2. 获取配置文件信息

方法一:

使用@ConfigurationProperties注解

@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {

    private String mpAppId;
    private String mpAppSecret;
}

方法二:

@Configuration
@Data
//@ConfigurationProperties(prefix = "project.url")
public class ProjectUrlConfig {
    
    @Value("${project.url.sell}")
    private String sell;
}

注意@Configuration注解是必须的,@Component同样适用

测试:

ProjectUrlConfig.getSell();//http://localhost:8080  

你可能感兴趣的:(SpringBoot)