springboot配置文件详解

一、简介SpringBoot使用一个全局的配置文件,配置文件名是固定的;     1.application.properties 
2.application.yml(或者是yaml) 配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好;YAML(YAML Ain't Markup Language)   YAML A Markup Language:是一个标记语言   YAML isn'tMarkup Language:不是一个标记语言;标记语言:以前的配置文件;大多都使用的是 xxxx.xml文件;YAML:以数据为中心,比json、xml等更适合做配置文件;

 

二.springbooot的properties类型配置文件:application.properties

Properties文件是java中很常用的一种配置文件,文件后缀为“.properties”,属文本文件,文件的内容格式是“键=值”的格式,
可以用“#”作为注释,java编程中用到的地方很多,运用配置文件,可以便于java深层次的解耦。
例如java应用通过JDBC连接数据库时,可以把数据库的配置写在配置文件 jdbc.properties:

#jdbc连接四大要素
driver=com.mysql.jdbc.Driverjdbc
Url=jdbc:mysql://localhost:3306/test
user=root
password=123456
#web项目启动端口号配置
server.port=8888
#freemarker后缀
spring.freemarker.suffix=.ftl

三、application.yml类型配置文件application.yml
默认创建工程的时候不是自带的,需要我们手动创建出来,application.yml的写法类似树形结构
1.基本语法以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的,
大小写敏感

server:
  port: 8088

spring:
    datasource:
        name: test
        url: jdbc:mysql://localhost:3306/test
        username: root
        password: xxx
        # 使用druid数据源
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true

  

四、使用@Value读取配置文件数据
application.yml:
server:  
    port: 9999
student:  
    name: xiaoming  
    classname: computer
application.properties:
student.classname=english
然后我们编写测试类TestConfigController的代码
@RestController
public class TestConfigController {    
    @Value("${student.name}")    
    private String studentName;    
    @Value("${student.classname}")    
    private String className; 
   
    @RequestMapping("/")    
    public String index(){        
        return "hello";   
     }   
   @RequestMapping("valueTest")    
   public String valueTest(){       
     return studentName+","+className;    
   }
}            

当同时存在application.yml和application.properties文件时,并且它们里面有相同配置项的时候,application.properties的优先级要高使用application.properties配置,
@Value中的值可以在IDEA工具中通过ctrl+鼠标左键进行定位跳转,而yml不能

五.使用@ConfigurationProperties读取配置文件数据

  可将配置文件中的信息映射成javabean

  

/**        
 * 将配置文件中配置的每一个属性的值,映射到这个组件中        
 * @ConfigurationProperties : 告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;        
 * prefix = "user":配置文件中哪个下面的所有属性进行一一映射        
 * 只有这个组件是容器中的组件,才能拥有容器提供的@ConfigurationProperties功能;        
 *        
 */
 
 @Component  //将User类注册到容器中
 @ConfigurationProperties(prefix = "user")
 public class User {    
   private int id;    
   private String username;    
   private String password;
   //get和set方法
 }

主要考虑使用场景,比如我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;

如果我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使@ConfigurationProperties;

在项目启动时添加配置参数:

java -jar springbootdemo7-0.0.1-SNAPSHOT.jar --server.port=8080

posted @ 2020-04-11 12:42  whhhd  阅读(365)  评论(0编辑  收藏  举报