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;
  }
}

猜你喜欢

转载自www.cnblogs.com/punkrocker/p/10632395.html