Spring Boot 加载application.properties顺序

1、准备四份application.properties

   

 a、项目根目录下config/application.properties ,内容为:  test.user.name = a

 b、项目根目录下 application.properties ,内容为:  test.user.name = b

 c、项目根目录下src/main/resources/config/application.properties ,内容为:  test.user.name = c

 d、项目根目录下src/main/resources/application.properties ,内容为:  test.user.name = d

2、读取application中内容的测试controller

 @Controller
 public class My Controller{
  @Autowired
  private ApplicationContext ctx;

  @GetMapping("/prop")
  @ResponseBody
  public String getName(){
    return ctx.getEnvironment().getProperty("test.user.name");
  }
}

3、首次显示 a,依次从a开始删除属性,则依次显示a,b,c,d

4、结论:

 加载顺序为第1点中的a,b,c,d的顺序,并且后面加载的不会覆盖前边加载的同名属性

猜你喜欢

转载自www.cnblogs.com/zitenghua/p/10437058.html