Spring Boot之程序中配置文件值获取

引言

之前我们说过,在配置文件中能够自定义配置很多数据,在我的工作中经常遇到这种场景,在配置文件中定义数据从而在程序中获取应用,从而避免了频繁修改代码。那么在Spring Boot项目中是如何获取配置文件中自定义的数据呢?带着这个问题,听我讲解。

获取配置文件中值的几种方式

获取配置文件中的值有以下几种方式:

  1. @ConfigurationProperties
  2. @Value
  3. @PropertySource

@ConfigurationProperties

Spring Boot项目中可以利用该注解,一次性注入整个对象的值,例如:

student:
        studentName: 张三
        age:  18
        gender:className: 高一一班
        active: true
        birthDay: 2001/01/23
        favorite:
                - swimming
                - running

entity

package com.frank.entity;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@ConfigurationProperties(prefix = "student") // 将配置文件中的student对象值注入该类
@Component// 将该类加入到Spring容器中
public class Student {

    private String studentName;

    private Integer age;

    private String gender;

    private String className;

    private Boolean active;

    private Date birthDay;

    private List<String> favorite;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public Integer getAge() {
        return age;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public Date getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(Date birthDay) {
        this.birthDay = birthDay;
    }

    public List<String> getFavorite() {
        return favorite;
    }

    public void setFavorite(List<String> favorite) {
        this.favorite = favorite;
    }

    @Override
    public String toString() {
        return "Student{" +
                "studentName='" + studentName + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", className='" + className + '\'' +
                ", active=" + active +
                ", birthDay=" + birthDay +
                ", favorite=" + favorite +
                '}';
    }
}

注意:

  • @ConfigurationProperties只针对于Spring容器中的组件才会生效。
  • @ConfigurationProperties要求属性必须有setter方法。

@Value

//@ConfigurationProperties(prefix = "student") // 将配置文件中的student对象值注入该类
@Component// 将该类加入到Spring容器中
public class Student {

    @Value("${student.studentName}")
    private String studentName;
    @Value("${student.age}")
    private Integer age;
    @Value("${student.gender}")
    private String gender;
    @Value("${student.className}")
    private String className;
    @Value("${student.active}")
    private Boolean active;
    @Value("${student.birthDay}")
    private Date birthDay;
  //  @Value("#{'${student.favorite}'.split(',')}")
    private List<String> favorite;

注意:@Value的参数必须为String 类型,因此对于特殊类型注意,否则会出现类型转换失败的错误。

@PropertySource

该注解是引入指定配置文件,配合@ConfigurationProperties 或@Value注解将该配置文件中的值注入到实体类中。
新建一个yml

student:
        studentName: 张三
        age: 18
        gender:className: 高一一班
        active: true
        birthDay: 2001/01/23
        favorite:
                - swimming
                - running
@PropertySource(value = {"classpath:student.properties"})
@ConfigurationProperties(prefix = "student") // 将配置文件中的student对象值注入该类
@Component// 将该类加入到Spring容器中
public class Student {

注意:

  • 该注解只支持properties,并不支持yml。
  • 使用properties文件是注意将编码改为ASCII.否则会出现中文乱码问题。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值