spirng-boot学习日记02----配置以及单元测试

spring boot配置文件

在resource文件下存在配置文件

application.properties 或者

application.yml

作用:修改springboot自动配置的默认值

@SpringBootApplication 已经帮我们做了很多配置文件的处理,也给了很多默认值,比如tomcat 8080端口等

主要学习yml

以数据为中心

如:

server:

port:8088

yml语法:

1基本语法

k:(空格)v 空格是必须的

层级关系:根据左对齐规则,同一个缩进表示同一级

如:

server:

  port: 8088

  path: /hello

表示port和path是同属于server

 

普通值:数字、字符串、布尔

k: v直接写

注意:字符串不用加单引号或双引号,如果使用了双引号,则表示不需要转义里面的特殊字符,单引号则会转移里面的特殊字符

 

Map:

根据缩进行k:v表达所属关系

a:

  aa: fsadf

  ab: dfsafew

b:

  ba: fewaf

  bb: fadfaf

或者行内写法 a: {aa: sdfafsa,ab: fasdf4e}

数组:

用-表示数组中的一个元素

如:

animal:

 -cat

 -dog

或者一行写法如: animal: (空格)[cat,dog]  中间用逗号分隔

 

例子:

比如我编写的配置文件如下:

testDto:
 name: dengwj
 chinese: true
 age: 24
 info:
  info1: 12
  info2: 13
  info3: fdaf
 birth: 2019/04/18
 info1: [12,54,97,3344]

表示我有一个实体类,分别有name、chinese、age、info、birth、info1属性

编写实体类

public class TestDto {
    private String name;
    private Boolean chinese;
    private Integer age;
    private Map info;
    private Date birth;
    private List<Integer> info1;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boolean getChinese() {
        return chinese;
    }

    public void setChinese(Boolean chinese) {
        this.chinese = chinese;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Map getInfo() {
        return info;
    }

    public void setInfo(Map info) {
        this.info = info;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public List<Integer> getInfo1() {
        return info1;
    }

    public void setInfo1(List<Integer> info1) {
        this.info1 = info1;
    }

    @Override
    public String toString() {
        return "TestDto{" +
                "name='" + name + '\'' +
                ", chinese=" + chinese +
                ", age=" + age +
                ", info=" + info +
                ", birth=" + birth +
                ", info1=" + info1 +
                '}';
    }
}

写完之后在类上添加注解

@ConfigurationProperties(prefix = "testDto")

表示读取配置文件,而prefix则表示要读取testDto节点

添加该依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

在写配置文件的时候可以提示对应实体类属性名称.

这里值得注意的是,这个实例必须是springboot容器管理的,才能使用,所以还要添加注解

@Component

 

2:单元测试,看看是否能读取配置文件信息

编写单元测试类(前提是已经依赖了单元测试

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

 

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootMain.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FirstTest {
    @Autowired
    private  TestDto dto;
    @Test
    public void test(){
        System.out.println(dto.toString());

    }
}

需要注意添加 @RunWith(SpringRunner.class)使用的是SpringRunner而非JUnit

@SpringBootTest(classes = SpringBootMain.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

classes属性为main程序源文件

 

结果

TestDto{name='dengwj', chinese=true, age=24, info={info1=12, info2=13, info3=fdaf}, birth=Thu Apr 18 00:00:00 CST 2019, info1=[12, 54, 97, 3344]}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值