SpringBoot 配置⽂件

1.配置文件引入

我们知道,Tocmat默认端口号是8080,但如果我们程序访问时这个端口号已经被其它进程使用了怎么办呢?这个时候们就需要修改端口号.

如果修改application.properties文件,如下:

server.port = 9090

观察日志,显示Tomcat启动端口号为9090:

 

2.配置文件的格式

Spring Boot 配置⽂件有以下三种:

  • application.properties 
  • application.yml 
  • application.yaml

注意:

  • yml 为yaml的简写,使⽤⽅式⼀样.
  • 当应⽤程序启动时, Spring Boot会⾃动从classpath路径找到并加载 application.properties 和 application.yaml 或者 application.yml ⽂件.
  • 理论上讲 .properties 和 .yml 可以并存在于⼀个项⽬中,当 .properties 和 .yml 并存时,两个配置都会加载. 如果配置⽂件内容有冲突, 则以 .properties 为主, 也就是 .properties 优先级更⾼.
  • 虽然理论上来讲 .properties 可以和 .yml 共存,但实际的业务当中,我们通常会采取⼀种统⼀的配置⽂件格式,这样可以更好的维护(降低故障率).

 

3.properties 配置⽂件

3.1 基本语法

  • properties 是以键值的形式配置的,key 和 value 之间是以"="连接的.
# 配置项目端口号
server.port=8080
#配置数据库连接信息
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

3.2 读取

  • 在项⽬中,想要主动的读取配置⽂件中的内容,可以使⽤ @Value 注解来实现
  • @Value 注解使⽤" ${} "的格式读取
##自定义配置
demo.key1 = hello,properties
@RestController
public class PropertiesController {
    //读取配置文件
    @Value("{demo.key1}")
    private String key1;

    @RequestMapping("/readKey")
    public String readKey(){
        return "读取到的配置项key1:"+key1;
    }
}

3.3 缺点

从上述配置key可以看出,properties配置文件中会有很多冗余的信息 

4.yml配置文件

4.1 基本语法

  • yml 是树形结构的配置⽂件,它的基础语法是"key: value".
  • key 和 value 之间使⽤英⽂冒号加空格的⽅式组成,空格不可省略
#修改端口号
server:
  port: 9092


#数据相关配置
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=utf8&useSSL=false
    username: root
    password: root


#yml 配置不同数据类型及 null

# Null,~代表null
null.value: ~

# "" 空字符串
#空格直接后⾯什么都不加就可以了, 但这种⽅式不直观, 更多的表⽰是使⽤引号括起来
empty.value: ''

注意:空格!!!!

 

yml 和 properties连接数据库的配置对⽐:

 显而易见,yml配置文件可以省去很多冗余信息

4.2 读取

  • yml 读取配置的⽅式和 properties 相同,使⽤@Value 注解即可
demo:
  key1: hello,yml
@RestController
public class YmlController {
    @Value("${demo.key1}")
    public String key1;

    @RequestMapping("/readYml")
    public String readYml(){
        return key1;
    }

}

 4.2.1 value 值加单双引号

string:
  str1: Hello \n Spring Boot.
  str2: 'Hello \n Spring Boot.'
  str3: "Hello \n Spring Boot."
@RestController
public class YmlController {

    @Value("${string.str1}")
    public String str1;

    @Value("${string.str2}")
    public String str2;

    @Value("${string.str3}")
    public String str3;


    @PostConstruct
    public void init(){

        System.out.println("str1:"+str1);
        System.out.println("str2:"+str2);
        System.out.println("str3:"+str3);
       
    }
}

运行结果如下:

 注意:

  • 单引号会转义特殊字符,使其失去特殊功能, 始终是⼀个普通的字符串
  • 双引号不会转义字符串⾥⾯的特殊字符, 特殊字符会表⽰本⾝的含义
  • \n 本意表⽰的是换⾏,使⽤单引号会转义, 就是说, \n 不再表⽰换⾏了, ⽽是表⽰⼀个普通的字符串 使⽤双引号不会转义, 表⽰\n表⽰的是它本⾝的含义, 就是换⾏

4.3 配置对象

student:
  id: 18
  name: zhangsan
  age: 12

读取: 

@Component 
@Data
@ConfigurationProperties(prefix = "student")
#这个时候就不能⽤ @Value 来读取配置中的对象了,使⽤另⼀个注解 @ConfigurationProperties 来读取 

public class Student {
    private Integer id;
    private String name;
    private Integer age;
}

此时就不能⽤ @Value 来读取配置中的对象了,使⽤注解 @ConfigurationProperties 来读取 

@RestController
public class YmlController {
    
    @Autowired
    public Student student;

    @PostConstruct
    public void init(){
        System.out.println("student:"+student);
    }
}

4.4 配置集合

dbtypes:
  name:
    - mysql
    - sqlserver
    - db2

注意-后面的空格,如果不加空格,这部分内容回变成一个整体,统一赋值给对应属性 

 读取:

@Component
@ConfigurationProperties(prefix = "dbtypes")
@Data
public class DBType {

    private String[] name;//可以用List接受

}

注入: 

@RestController
public class YmlController {

    @Autowired
    public DBType dbType;

    @PostConstruct
    public void init(){
        System.out.println("dbtype:"+dbType);
    }
}

 

4.5 配置Map 

dbtypes:
  map:
    k1: kk1
    k2: kk2
    k3: kk3

读取: 

@Component
@ConfigurationProperties(prefix = "dbtypes")
@Data
public class DBType {

    private HashMap<String,String> map;
}
@RestController
public class YmlController {
   
    @PostConstruct
    public void init(){

        System.out.println("dbtype:"+dbType);
    }
}

4.6 优缺点

优点:

  1. 可读性⾼,写法简单, 易于理解.
  2. ⽀持更多的数据类型, 可以简单表达对象, 数组, List,Map等数据形态.
  3. ⽀持更多的编程语⾔.

缺点:

  1. 不适合写复杂的配置⽂件                                                                                                        
  2. 对格式有较强的要求(⼀个空格可能会引起⼀场⾎案)

5.配置⽂件作⽤

  • 解决硬编码带来的问题, 把可能会发⽣改变的信息, 放在⼀个集中的地⽅, 当我们启动某个程序时, 应⽤程序从配置⽂件中读取数据, 并加载运⾏.
  • 可以使程序完成⽤⼾和应⽤程序的交互, 或者应⽤程序与其他应⽤程序的交互
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值