怎么在Springboot中引入多个yml

  介绍

怎么在Springboot中引入多个yml ?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

Springboot是什么

Springboot一种全新的编程规范,其设计目的是用来简化新弹簧应用的初始搭建以及开发过程,Springboot也是一个服务于框架的框架,服务范围是简化配置文件。

Springboot默认加载的是application.yml文件,所以想要引入其他配置的yml文件,就要在application.yml中激活该文件

定义一个application-resources.yml文件(注意:必须以应用程序——开头)

application.yml中:

,春天:
,配置文件:
,,,活动:资源

以上操作,xml自定义文件加载完成,接下来进行注入。

应用程序资源。yml配置文件代码:

用户:   12346年,filepath:   ,uname:,“13”;      管理:   以前,家乡:26日

方案一:无前缀,使用@ value注解

@ component//@ConfigurationProperties (=prefix “user")   public  class  User  {   @ value才能(“$ {user.filepath}“)   private 才能;String  filepath;   @ value才能(“$ {user.uname}“)   private 才能;String  uname;   public 才能;String  getFilepath (), {   ,,,return  filepath;   ,,}   public 才能;void  setFilepath (String  filepath), {   ,,,this.filepath =, filepath;   ,,}   public 才能;String  getUname (), {   ,,,return  uname;   ,,}   public 才能;void  setUname (String  uname), {   ,,,this.uname =, uname;   ,,}   @Override才能   public 才能;String  toString (), {   ,,,return “用户{“+   ,,,,,,,“filepath=& # 39;“, +, filepath  +, & # 39; \ & # 39; & # 39; +   ,,,,,,,,,,uname=& # 39;“, +, uname  +, & # 39; \ & # 39; & # 39; +   ,,,,,,,& # 39;}& # 39;;   ,,}   }

方案二:有前缀,无需@ value注解

@ component   @ConfigurationProperties (=prefix “user")   public  class  User  {//@ value才能(“$ {user.filepath}“)   private 才能;String  filepath;//@ value才能(“$ {user.uname}“)   private 才能;String  uname;   public 才能;String  getFilepath (), {   ,,,return  filepath;   ,,}   public 才能;void  setFilepath (String  filepath), {   ,,,this.filepath =, filepath;   ,,}   public 才能;String  getUname (), {   ,,,return  uname;   ,,}   public 才能;void  setUname (String  uname), {   ,,,this.uname =, uname;   ,,}   @Override才能   public 才能;String  toString (), {   ,,,return “用户{“+   ,,,,,,,“filepath=& # 39;“, +, filepath  +, & # 39; \ & # 39; & # 39; +   ,,,,,,,,,,uname=& # 39;“, +, uname  +, & # 39; \ & # 39; & # 39; +   ,,,,,,,& # 39;}& # 39;;   ,,}   }

测试类:

package  com.sun123.springboot;   import  org.junit.Test;   import  org.junit.runner.RunWith;   import  org.springframework.beans.factory.annotation.Autowired;   import  org.springframework.boot.test.context.SpringBootTest;   import  org.springframework.test.context.junit4.SpringRunner;   @RunWith (SpringRunner.class)   @SpringBootTest   public  class  UTest  {   @ autowired才能   User 才能;用户;   @Test才能   public 才能;void  test01 () {   ,,,System.out.println(用户);   ,,}   }

测试结果:

怎么在Springboot中引入多个yml

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。

怎么在Springboot中引入多个yml