黑马_13 Spring Boot:04.spring boot 配置文件

13 Spring Boot:html

01.spring boot 介绍&&02.spring boot 入门mysql

04.spring boot 配置文件web

05.spring boot 整合其余技术spring

 SpringBoot基础 


4、SpringBoot的配置文件

SpringBoot配置文件类型和做用 sql

SpringBoot是基于约定的,因此不少配置都有默认值,但若是想使用本身的配置替换默认配置的话,就可使用
application.properties 或者 application.yml application.yaml)进行配置。
编程

  application.properties数组

 

#tomcat服务器的端口号
server.port=8888
#当前web应用的名称
server.servlet.context-path=

 

其中,application.properties文件是键值对类型的文件,以前一直在使用,因此此处不在对properties文件的格式进行阐述。
除了properties文件外,SpringBoot还可使用yml文件进行配置,下面对yml文件进行讲解。

tomcat

 4.1.2 application.yml配置文件


  
yml配置文件简介springboot

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

YML文件的扩展名可使用.yml或者.yaml
服务器

 

 

 配置普通数据
 语法: key: value

 

name: haohao 

注意:value以前有一个空格

配置对象数据

person:
    name: haohao
    age: 31
    addr: beijing
#或者
person: {name: haohao,age: 31,addr: beijing} 

注意:key1前面的空格个数不限定,在yml语法中,相同缩进表明同一个级别


 

 配置数组(ListSet)数据

语法:
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与之间的 - 之间存在一个空格

4.1.3 SpringBoot配置信息的查询

上面说起过,SpringBoot的配置文件,主要的目的就是对配置信息进行修改的,但在配置时的key从哪里去查询
呢?咱们能够查阅SpringBoot的官方文档

 文档URLhttps://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#common-applicationproperties

# ----------------------------------------
# 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. Autodetected 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.

 

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

  4.2.1 使用注解@Value映射
  咱们能够经过@Value注解将配置文件中的值映射到一个Spring管理的Bean的字段上

 

 Controller层实体Bean代码以下:
 

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

 

 4.2.2 使用注解@ConfigurationProperties映射

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

  

        <!-- @ConfigurationProperties的执行器的配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

 

 实体Bean代码以下:
 

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

 

 注意:使用@ConfigurationProperties方式能够进行配置文件与实体字段的自动映射,但须要字段必须提供set方法才能够。



本文不使用application.properties文件 而使用更加简洁的application.yml文件。将resource文件夹下原有的application.properties文件删除,建立application.yml配置文件(备注:其实SpringBoot底层会把application.yml文件解析为application.properties),因而建立了两个yml文件(application.yml和application-dev.yml)。由于如今一个项目有好多环境:开发环境,测试环境,准生产环境,生产环境,每一个环境的参数能够有不一样,因此咱们就能够把不一样环境下的参数配置到各自对应的yml文件中,这样在想用哪一个环境的配置项时候只须要在主配置文件中将用到的配置文件写上就行。

笔记:在Spring Boot中多环境配置文件名须要知足application-{profile}.yml的格式,其中{profile}对应你的环境标识,好比:

application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境

至于哪一个具体的配置文件会被加载,须要在application.yml文件中经过spring.profiles.active属性来设置,其值对应{profile}值。

application.yml

spring:
  profiles:
    active: dev

application-dev.yml

 

server:
  port: 8080
 
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
 
mybatis:
  mapper-locations: classpath*:cn/bjut/mapper/*Mapper.xml
  type-aliases-package: cn.bjut.entity,cn.bjut.domain,cn.bjut.pojo
 
#showSql
logging:
  level:
    cn:
      bjut:
        mapper : debug

 

====================

参考资料:

把每一个不一样运行环境的参数配置到.yml文件中

end

相关文章
相关标签/搜索