• SpringBoot_02_SpringBoot的配置文件


    1.SpringBoot配置文件

    SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用application.properties或者application.yml(application.yaml)进行配置。

    SpringBoot默认会从Resources目录下加载application.properties或application.yml(application.yaml)文件

    1.1 yml配置文件简介

    YML文件格式是YAML (YAML Aint Markup Language)编写的文件格式,YAML是一种直观的能够被电脑识别的的数据数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,可以被支持YAML库的不同的编程语言程序导入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP等。YML文件是以数据为核心的,比传统的xml方式更加简洁。

    2.1 配置普通数据

    • 语法: key: value

    • 示例代码:

    • name: haohao
    • 注意:value之前有一个空格

    2.2 配置对象数据

    • 语法:

      key:

      key1: value1

      key2: value2

      或者:

      key: {key1: value1,key2: value2}

    • 示例代码:

    • person:
      name: haohao
      age: 31
      addr: beijing

      #或者

      person: {name: haohao,age: 31,addr: beijing}
    • 注意:key1前面的空格个数不限定,在yml语法中,相同缩进代表同一个级别

    2.3配置Map数据

    同上面的对象写法

    2.4配置数组(List、Set)数据

    • 语法:

      key:

      - value1

      - value2

      或者:

      key: [value1,value2]

    • 示例代码:

    • city:
      - beijing
      - tianjin
      - shanghai
      - chongqing
       
      #或者

      city: [beijing,tianjin,shanghai,chongqing]

      #集合中的元素是对象形式
      student:
      - name: zhangsan
        age: 18
        score: 100
      - name: lisi
        age: 28
        score: 88
      - name: wangwu
        age: 38
        score: 90
    • 注意:value1与之间的 - 之间存在一个空格

    Spring官网提供了了大量的配置信息例如Redis的Spring Boot的配置文件

    https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#common-application-properties

    常用的配置如下:

    # QUARTZ SCHEDULER (QuartzProperties)
    spring.quartz.jdbc.initialize-schema=embedded # Database schema initialization mode.
    spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
    spring.quartz.job-store-type=memory # Quartz job store type.
    spring.quartz.properties.*= # Additional Quartz Scheduler properties.
    
    # ----------------------------------------
    # WEB PROPERTIES
    # ----------------------------------------
    
    # EMBEDDED SERVER CONFIGURATION (ServerProperties)
    server.port=8080 # Server HTTP port.
    server.servlet.context-path= # Context path of the application.
    server.servlet.path=/ # Path of the main dispatcher servlet.
    
    # HTTP encoding (HttpEncodingProperties)
    spring.http.encoding.charset=UTF-8 # Charset of HTTP requests and responses. Added to the "Content-Type" header if not set explicitly.
    
    # JACKSON (JacksonProperties)
    spring.jackson.date-format= # Date format string or a fully-qualified date format class name. For instance, `yyyy-MM-dd HH:mm:ss`.
    
    # SPRING MVC (WebMvcProperties)
    spring.mvc.servlet.load-on-startup=-1 # Load on startup priority of the dispatcher servlet.
    spring.mvc.static-path-pattern=/** # Path pattern used for static resources.
    spring.mvc.view.prefix= # Spring MVC view prefix.
    spring.mvc.view.suffix= # Spring MVC view suffix.
    
    # DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
    spring.datasource.driver-class-name= # Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
    spring.datasource.password= # Login password of the database.
    spring.datasource.url= # JDBC URL of the database.
    spring.datasource.username= # Login username of the database.
    
    # JEST (Elasticsearch HTTP client) (JestProperties)
    spring.elasticsearch.jest.password= # Login password.
    spring.elasticsearch.jest.proxy.host= # Proxy host the HTTP client should use.
    spring.elasticsearch.jest.proxy.port= # Proxy port the HTTP client should use.
    spring.elasticsearch.jest.read-timeout=3s # Read timeout.
    spring.elasticsearch.jest.username= # Login username.

     3. 配置文件与配置类的属性映射方式

    常用方式

    3.1使用注解@Value映射

    application.properties配置如下:

    person:
      name: zhangsan
      age: 18

    获取方式一:

    @Controller
    public class QuickStartController {
    
        @Value("${person.name}")
        private String name;
        @Value("${person.age}")
        private Integer age;
    
    
        @RequestMapping("/quick")
        @ResponseBody
        public String quick(){
            return "springboot 访问成功! name="+name+",age="+age;
        }
    
    }

     3.2使用注解@ConfigurationProperties映射

     通过注解@ConfigurationProperties(prefix="配置文件中的key的前缀")可以将配置文件中的配置自动与实体进行映射

    application.properties配置如下

    person:
      name: zhangsan
      age: 18

    获取方式二

    @Controller
    @ConfigurationProperties(prefix = "person")
    public class QuickStartController {
    
        private String name;
        private Integer age;
    
        @RequestMapping("/quick")
        @ResponseBody
        public String quick(){
            return "springboot 访问成功! name="+name+",age="+age;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    }

    <你是我自罚三杯也不肯开口的秘密>

     

     

     

  • 相关阅读:
    最炫数学风
    A fine property of the convective terms of axisymmetric MHD system, and a regularity criterion in terms of $om^ t$
    Regularity criteria for NSE 4: $p_3u$
    2017-2018-2 PDE 讨论班
    2017-2018-2点集拓扑
    轴对称 Navier-Stokes 方程组的点态正则性准则 II
    轴对称 Navier-Stokes 方程组的点态正则性准则 I
    二维空间中的一个向量场的散度
    word插入公式不自动斜体的解决办法
    2017-2018-2课表
  • 原文地址:https://www.cnblogs.com/asndxj/p/11826896.html
Copyright © 2020-2023  润新知