【spring boot笔记】3.配置文件的值注入

这篇笔记主要是记录yml配置文件传递值的过程

1.创建一个spring boot的Web应用

https://blog.csdn.net/AXIMI/article/details/88649270

2.创建全局配置文件application.yml

路径结构如下图所示:
在这里插入图片描述
application.yml的代码:

server:
  port: 8081
  path: /hello
person:
  name: Tom
  age: 8
  birthday: 2010/10/2
  maps: {k1: v1,k2: v2}
  lists:
    - Tom
    - Mary
    - Jack
  dog:
    name: lucky
    age: 1

// yml语言的书写规则:https://blog.csdn.net/AXIMI/article/details/88649270

3.创建两个类

在这里插入图片描述
目录结构如上图所示,我们需要创建两个类,Person和Dog
Person的代码如下:

package com.demo.learning02.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import java.util.*;

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
    private Date birthday;

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }
}

类的上方有两个注解:

  • @ConfigurationProperties(prefix = “person”) 可以从全局配置文件中获取值
  • 只有容器中的组件才提供@ConfigurationProperties 的功能,所以还要加上@Component

此外,@ConfigurationProperties的功能也可以通过@Value注解来实现。例如,对于上面的代码,也可以写为:
在这里插入图片描述
@ConfigurationProperties和@Value的区别:

@ConfigurationProperties@Value
支持松散绑定(松散语法)不支持
不支持支持SpEL(Spring的表达式),
例如"#{2*8}"
支持JSR303数据校验(@Validated)不支持
支持复杂类型封装(例如map)不支持复杂类型封装

Dog的代码如下:
package com.demo.learning02.bean;

public class Dog {
    private String name;
    private Integer age;

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

4.导入配置文件处理器依赖

在pom.xml文件中添加依赖:

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

5.进行单元测试

在根目录/src/test/java文件夹下面有单元测试的代码,目录结构如下:
在这里插入图片描述
例如我这里单元测试的代码文件是Learning02ApplicationTest2。打开这个文件进行修改,最终的单元测试代码为:

package com.demo.learning02;

import com.demo.learning02.bean.Person;
import org.junit.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
public class Learning02ApplicationTests {
    @Autowired
    Person person;

    @Test
    public void contextLoads() {
        System.out.println(person);
    }

}

类的上方有两个注解:

  • @SpringBootTest 说明这是个单元测试类
  • @RunWith(SpringRunner.class)说明单元测试使用的是spring的启动器

运行单元测试代码后,可以看到:
在这里插入图片描述
成功将yml配置文件中的数据传给了person实例。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值