4springboot的yml文件使用

1yml文件语法

1.1yml文件

server:
  port: 8080

person:
    lastName: hello
    age: 18
    boss: false
    birth: 2017/12/12
    maps: {k1: v1,k2: 12}
    lists:
     ‐ lisi
     ‐ zhaoliu
    dog:
     name: 小狗
     age: 12

1.2实体类


/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
 *      prefix = "person":配置文件中哪个下面的所有属性进行一一映射
 *
 * 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
 *  @ConfigurationProperties(prefix = "person")默认从全局配置文件中获取值;
 *
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    
    private Integer age;

    private Boolean boss;

    private Date birth;

    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

}

1.3测试

package com.example.junit.Controller;

import com.example.junit.pojo.Person;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
class HelloControllerTest {


    @Autowired
    Person person;


    @Test
    void print() {
        System.out.print(person);
    }
}

运行:

Person{lastName='hello', age=18, boss=false, birth=Tue Dec 12 00:00:00 CST 2017, maps={k1=v1, k2=12}, lists=[‐ lisi ‐ zhaoliu], dog=Dog{name='小狗', age=12}}

2yml文件和 properties转换

application.yml

server:
  port: 8080

person:
    lastName: hello
    age: 18
    boss: false
    birth: 2017/12/12
    maps: {k1: v1,k2: 12}
    lists:
     ‐ lisi
     ‐ zhaoliu
    dog:
     name: 小狗
     age: 12

application.properties:

person.last-name=李四
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15

注意:
properties配置文件在idea中默认utf-8可能会乱码
idea 的编码时utf-8
propertis 的编码时ascli
修改:
在这里插入图片描述

3@Value

3.1application. properties

person.last-name=李四
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15

3.2使用

@Component
public class Person2 {


    @Value("${person.last-name}")
    private String lastName;
    @Value("#{11*2}")
    private Integer age;
    @Value("true")
    private Boolean boss;

    private Date birth;
    //@Value("${person.maps}")不支持复杂类型
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

3.3@Value获取值和@ConfigurationProperties获取值比较

在这里插入图片描述
配置文件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value; 如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;

4加载指定的配置文件

4.1PropertySource

在这里插入图片描述
@PropertySource(value = {“classpath:person.properties”})
在这里插入图片描述

4.2@Configuration

使用@Bean给容器中添加组件

package com.atguigu.springboot.config;


import com.atguigu.springboot.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件
 *
 * 在配置文件中用<bean><bean/>标签添加组件
 *
 */
@Configuration
public class MyAppConfig {

    //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
    @Bean
    public HelloService helloService02(){
        System.out.println("配置类@Bean给容器中添加组件了...");
        return new HelloService();
    }

}

5profile

5.1第一种方式

在这里插入图片描述
在这里插入图片描述

5.2 yml支持多文档块方式

server:
  port: 8081
spring:
  profiles:
active: prod
‐‐‐ 
server:
  port: 8083
spring:
  profiles: dev
‐‐‐
server:
  port: 8084
spring:
profiles: prod #指定属于哪个环境
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值