• SpringBoot读取配置详解


    一、springboot配置文件

            核心配置文件和自定义配置文件。核心配置文件是指在resources根目录下的application.propertiesapplication.yml配置文    件。为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义

       信息,这里在resources/config目录下创建配置文件config.properties

    二、核心配置文件的两种读取方法

       核心配置文件内容:

    1 server:
    2   profiles: 
    3     active: dev4 
    5 test:
    6   hello: hello

    (1)@Value("${键名}")

     1 @RestController
     2 @RequestMapping("/hello")
     3 public class PushController extends BaseController {
     4 
     5     @Value("${test.string}")
     6     private String hello;
     7 
     8     @RequestMapping("/test")
     9     public void test() {
    10         System.out.println("spring boot !:" + hello);
    11     }
    12 
    13 }

    (2)使用Environment,env.getProperty("键名")

      由于Enviroment有几种包,所以给出正确jar包地址:

      import org.springframework.core.env.Environment;
     1 @RestController
     2 @RequestMapping("/hello")
     3 public class PushController extends BaseController {
     4 
     5     @Autowired
     6     private Environment env;
     7 
     8 
     9     @RequestMapping("/test")
    10     public void test() {
    11         System.out.println("spring boot !:" + env.getProperty("test.string"));
    12     }
    13 
    14 }

    三、自定义配置文件的读取方法

           自定义的config.properties的内容如下:

    1 project.version=1.0-SNAPSHOT
    2 project.name=boot

       创建实体类MyConfig。

            注意:springboot1.5版本以下@ConfigurationProperties有两个属性locations(指定配置文件的所在位置),       

           prefix(指定配置文件中键名称的前缀)。即@ConfigurationProperties(prefix = "project",locations = "classpath:config/config.properties")

      但是1.5版本及以上的版本取消了locations属性,为了指定配置文件的

           位置,使用@PropertySource(value = "自定义配置文件路径")指定文件所在位置。

     1 @Component
     2 @ConfigurationProperties(prefix = "project")
     3 @PropertySource(value = "classpath:config/config.properties")
     4 public class MyConfig {
     5     private String version;
     6     private String name;
     7 
     8     public String getVersion() {
     9         return version;
    10     }
    11 
    12     public void setVersion(String version) {
    13         this.version = version;
    14     }
    15 
    16     public String getName() {
    17         return name;
    18     }
    19 
    20     public void setName(String name) {
    21         this.name = name;
    22     }
    23 }
     1 @RestController
     2 public class HelloController {
     3 
     4     @Autowired
     5     private MyConfig myConfig;
     6 
     7     @RequestMapping(value = "/hello")
     8     public String hello() {
     9         return "hello spring boot! " myConfig.getVersion() + " " +  myConfig.getName() ;
    10     }
    11 }
  • 相关阅读:
    CF1280G Kirchhoff's Current Loss【表达式解析,不等式】
    [AGC040C] Neither AB nor BA
    [AGC040B]Two Contests
    [ARC101E]Ribbons on Tree(容斥,dp)
    [GXOI/GZOI2019]旧词
    [SDOI2015]寻宝游戏
    半平面交初步
    [CF585E]Marbles
    [P5348]密码解锁
    NOIP2018 保卫王国
  • 原文地址:https://www.cnblogs.com/huanglog/p/8780885.html
Copyright © 2020-2023  润新知