springboot读取application.properties中自定义配置

假设在application-xxx.properties中配置

user.name=yuhk

 

一、在Controller中读取

@Value("{$user.name}")

private String userName;

 

二、static属性读取

首先,要给类加上@Component注解

然后,在属性的set方法上加入@Value注解

例:

@Component
public class Hi {
  public static String userName;
  @Value("${user.name}")
  public String setUserName(String name) {
    this.userName = name;
  }
}

 

posted @ 2019-03-31 18:11  朋克  阅读(643)  评论(0编辑  收藏  举报