广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot 如何从配置文件读取值到对象中
  • 840
分享到

SpringBoot 如何从配置文件读取值到对象中

2024-04-02 19:04:59 840人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录一、实现方式@ConfigurationProperties 注解@Valid注解二、两者区别三、代码演示四、@PropertySource 读取指定配置文件五、@ImportR

一、实现方式

@ConfigurationProperties 注解

(最好加上前缀prefix=“person”,标明是和配置文件中哪个开头的属性匹配)

推荐使用用在类上,从配置文件读取属性值,放到对象里面,复杂的结构也适用例如map,list,对象。支持校验:@Validated

@Valid注解

用在属性上,需要每个属性逐个绑定通过@value注解获取配置文件的值,不适合做复杂类型(map,list ,对象)值得获取不支持@Validated

二、两者区别

在这里插入图片描述

三、代码演示

使用@ConfigurationProperties注解


package com.wx.SpringBoot20190911.demo.model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component//perosn 需要纳入spring ioc 容器里
@ConfigurationProperties(prefix = "person")//使用前缀标明具体的属性
@Validated
public class Person {
    @Email
    String email;
    String hello;
    String name;
    int age;
    boolean boss;
    Date birth;
    Map<String,String> maps;
    List<String> list;
    Dog dog;
    @Override
    public String toString() {
        return "Person{" +
                "email='" + email + '\'' +
                ", hello='" + hello + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", list=" + list +
                ", dog=" + dog +
                '}';
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getHello() {
        return hello;
    }
    public void setHello(String hello) {
        this.hello = hello;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public boolean isBoss() {
        return boss;
    }
    public void setBoss(boolean boss) {
        this.boss = boss;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    public Map<String, String> getMaps() {
        return maps;
    }
    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
}
package com.wx.springboot20190911.demo.model;
public class Dog {
    String name;
    String color;
    int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                ", age=" + age +
                '}';
    }
}

使用@Value注解


package com.wx.springboot20190911.demo.model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component//perosn 需要纳入spring ioc 容器里
//@ConfigurationProperties(prefix = "person")
@Validated
public class Person1 {
    @Email
    @Value("${person1.email}")
    String email;
    @Value("${person1.hello}")
    String hello;
    @Value("${person1.name}")
    String name;
    @Value("#{12*3}")//支持计算
    int age;
    @Value("${person1.boss}")
    boolean boss;
    @Value("${person1.birth}")
    Date birth;
    Map<String,String> maps;
    List<String> list;
    Dog dog;
    public String getHello() {
        return hello;
    }
    public void setHello(String hello) {
        this.hello = hello;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public boolean isBoss() {
        return boss;
    }
    public void setBoss(boolean boss) {
        this.boss = boss;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    public Map<String, String> getMaps() {
        return maps;
    }
    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    @Override
    public String toString() {
        return "Person1{" +
                "email='" + email + '\'' +
                ", hello='" + hello + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", list=" + list +
                ", dog=" + dog +
                '}';
    }
}

配置类(application.properties)代码


person.hello=luck
person.name=吴
#乱码的话 就setting设置下file encoding
person.list=ww,xx,rr
person.maps.k1=v1
person.maps.k2=v2
person.dog.name=cat
person.dog.color=red
person.dog.age=1
person.age=12
person.birth=2019/01/11
person.boss=false
person.email=www@qq.com
person1.hello=luck
person1.name=吴
#乱码的话 就setting设置下file encoding
person1.list=ww,xx,rr
person1.age=12
person1.birth=2019/01/11
person1.boss=false
person1.email=www

配置类(application.yml)代码:这种方式更加结构化


person:
  name: 霞
  age: 16
  boss : false
  birth: 2012/09/12
  maps: {k1: v1,k2: v2}
  list: [dog,cat ,house,rabbits]
  dog:
    name: ${person.hello}
    age: ${random.int(10)}
    color: white
  hello: yula

打印结果:使用第一种方式时,如果email不是"www@qq.com"这种格式,是不能运行成功的,但是使用@Value 不会校验,如下面是"www",一样能运行成功

Person{email='www@qq.com', hello='luck', name='吴', age=12, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps={k1=v1, k2=v2}, list=[ww, xx, rr], dog=Dog{name='cat', color='red', age=1}}

Person1{email='www', hello='luck', name='吴', age=36, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps=null, list=null, dog=null}

四、@PropertySource 读取指定配置文件


package com.wx.springboot20190911.demo.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;

@PropertySource(value={"classpath:person.properties"})//指定读取person.properties配置文件
@Component//perosn 需要纳入spring ioc 容器里
@ConfigurationProperties(prefix = "person")//使用前缀标明具体的属性
@Validated
public class Person2 {
    @Email
    String email;
    String hello;
    String name;
    int age;
    boolean boss;
    Date birth;
    Map<String,String> maps;
    List<String> list;
    Dog dog;
    @Override
    public String toString() {
        return "Person{" +
                "email='" + email + '\'' +
                ", hello='" + hello + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", list=" + list +
                ", dog=" + dog +
                '}';
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getHello() {
        return hello;
    }
    public void setHello(String hello) {
        this.hello = hello;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public boolean isBoss() {
        return boss;
    }
    public void setBoss(boolean boss) {
        this.boss = boss;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    public Map<String, String> getMaps() {
        return maps;
    }
    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
}

这里虽然指定了读取person.properties配置文件,但是由于prefix=“person”,导致还是读取了application.properties配置文件,因为application.properties权限最高,要想读取person.properties配置文件,就得改前缀名,例如改成prefix=“person2” person.properties配置文件的内容如下:

在这里插入图片描述

没改前缀名的结果演示:

Person{email='www@qq.com', hello='luck', name='吴', age=12, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps={k1=v1, k2=v2}, list=[ww, xx, rr], dog=Dog{name='cat', color='red', age=1}}

Person1{email='www', hello='luck', name='吴', age=36, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps=null, list=null, dog=null}

Person{email='www@qq.com', hello='luck', name='吴', age=12, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps={k1=v1, k2=v2}, list=[ww, xx, rr], dog=Dog{name='cat', color='red', age=1}}

改前缀名的代码演示:

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

改了前缀名的结果演示:这时候读取的就是指定的配置文件的值

Person{email='www@qq.com', hello='luck', name='吴', age=12, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps={k1=v1, k2=v2}, list=[ww, xx, rr], dog=Dog{name='cat', color='red', age=1}}

Person1{email='www', hello='luck', name='吴', age=36, boss=false, birth=Fri Jan 11 00:00:00 CST 2019, maps=null, list=null, dog=null}

Person{email='12345@qq.com', hello='springboot', name='指定读取配置文件', age=12, boss=false, birth=Wed Sep 11 00:00:00 CST 2019, maps={k2=v2, k1=v1}, list=[@Value, @PropertySource], dog=Dog{name='cat', color='red', age=1}}

注:只能读取 .properties 文件,无法读取 .yml 文件

五、@ImportResource:导入Spring配置文件

让配置文件里面的内容生效

Springboot 里没有Spring 的配置文件,我们自己编写的配置文件,也不能自动识别;

想让Spring的配置文件生效,加载进来,需使用该注解

写一个Person3 类,如图所示:

在这里插入图片描述

自定义一个Spring配置文件,如图所示:

在这里插入图片描述

在启动类上注解

在这里插入图片描述

测试:自定义的这个配置文件是否生效

在这里插入图片描述

演示结果:生效了

2019-09-11 22:15:12.247 INFO 7824 --- [ main] c.w.s.demo.DemoApplicationTests : Started DemoApplicationTests in 8.558 seconds (JVM running for 10.882)

Person{email='null', hello='null', name='null', age=0, boss=false, birth=null, maps=null, list=null, dog=null}

六、思维导图

在这里插入图片描述

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: SpringBoot 如何从配置文件读取值到对象中

本文链接: https://www.lsjlt.com/news/156156.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringBoot 如何从配置文件读取值到对象中
    目录一、实现方式@ConfigurationProperties 注解@Valid注解二、两者区别三、代码演示四、@PropertySource 读取指定配置文件五、@ImportR...
    99+
    2022-11-12
  • SpringBoot yml配置文件如何读取
    本篇内容主要讲解“SpringBoot yml配置文件如何读取”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot yml配置文件如何读取”吧!yaml介绍YA...
    99+
    2023-07-04
  • springboot如何读取配置文件到静态工具类
    目录springboot读取配置文件到静态工具类我们可以用Environment 来解决将配置文件的值加载到工具类的静态变量中(多环境运行加载)首先创建一个SpringBoot项目创...
    99+
    2022-11-12
  • SpringBoot如何读取配置文件中的数据到map和list
    目录读取配置文件中的数据到map和listspringboot读取配置文件中的配置信息到mapspringboot读取配置文件中的配置信息到list测试上述配置是否有效配置文件的读取...
    99+
    2022-11-13
  • SpringBoot如何读取外部配置文件
    这篇文章将为大家详细讲解有关SpringBoot如何读取外部配置文件,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。1.SpringBoot配置文件SpringBoot使用一个以application命名的...
    99+
    2023-06-29
  • java springboot中如何读取配置文件的属性
    目录配置文件(1)使用注解@Value映射(2)使用@ConfigurationProperties映射(3)推荐使用:极简方式 @Bean和@ConfigurationProper...
    99+
    2022-11-13
  • springboot logback如何从apollo配置中心读取变量
    目录springbootlogback从apollo配置中心读取变量1、在apollo配置中心添加2、项目的application.yml配置文件配置如下3、在logback.xml...
    99+
    2022-11-12
  • SpringBoot怎么读取配置文件中的数据到map和list
    今天小编给大家分享一下SpringBoot怎么读取配置文件中的数据到map和list的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一...
    99+
    2023-06-29
  • springboot如何使用@Value获取配置文件的值
    使用@Value获取配置文件的值 1、创建配置文件(application.properties) spring.activemq.broker-url=tcp://localh...
    99+
    2022-11-12
  • C#中如何读取配置文件
    C#中如何读取配置文件,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。C#读取配置文件1、读取配置信息下面是一个配置文件的具体内容:   &nb...
    99+
    2023-06-18
  • .NetCore中如何读取配置文件
    小编给大家分享一下.NetCore中如何读取配置文件,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!在应用程序开发中,配置文件是主要存储系统的初始配置信息,配置文件的读取虽然属于基础内容却又经常用到,所以百丈高楼平地起,学习...
    99+
    2023-06-29
  • 如何使用springboot配置和占位符获取配置文件中的值
    小编给大家分享一下如何使用springboot配置和占位符获取配置文件中的值,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!springboot配置和占位符获取配置文件值@PropertySource& 加载指定的配...
    99+
    2023-06-29
  • 在SpringBoot下如何读取自定义properties配置文件
    这篇文章将为大家详细讲解有关在SpringBoot下如何读取自定义properties配置文件,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。SpringBoot工程默认读取application.prop...
    99+
    2023-05-30
    spring boot properties
  • 如何在C#中读取配置文件
    如何在C#中读取配置文件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。C#读取配置文件1.了解配置文件概述:应 用程序配置文件是标准的 XML 文件,XML 标...
    99+
    2023-06-18
  • 使用springboot如何实现获取配置文件中的属性值
    本篇文章给大家分享的是有关使用springboot如何实现获取配置文件中的属性值,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。在spring boot中,简单几步,读取配置文件...
    99+
    2023-05-31
    springboot 配置文件
  • bootstrap.yml如何读取nacos配置中心的配置文件
    目录bootstrap.yml读取nacos配置中心配置文件依赖的版本交由父模块进行版本控制了(另附上依赖代码)父模块的依赖管理总结bootstrap.yml读取nacos配置中心配...
    99+
    2022-12-28
    bootstrap.yml nacos配置中心 nacos配置文件
  • Springboot 如何指定获取出 yml文件里面的配置值
    之前写过一篇获取properties文件里面的值: Springboot 指定获取自己写的配置properties文件的值 www.jb51.net/article/217899.h...
    99+
    2022-11-12
  • Springboot 如何指定获取自己写的配置properties文件的值
    获取yml的可以参考这篇: Springboot 指定获取出 yml文件里面的配置值 www.jb51.net/article/217901.htm 直接进入正题: 先创建一个 配置...
    99+
    2022-11-12
  • PHP中如何读取和操作对象数组文件?
    在PHP中,对象和数组是两种非常重要的数据类型。当我们需要将对象和数组保存到文件中时,我们可以使用对象数组文件。本文将介绍如何在PHP中读取和操作对象数组文件。 什么是对象数组文件 对象数组文件是一种将对象和数组保存到文件中的数据结构。在P...
    99+
    2023-08-26
    对象 数组 文件
  • 如何使用Python读取大数据中的对象文件?
    在当今的数据时代,大数据已经成为了一种趋势。大数据存储的文件类型也越来越多,其中对象文件是一种常见的文件类型。Python作为一种流行的编程语言,自然也可以用来读取大数据中的对象文件。 本文将介绍如何使用Python读取大数据中的对象文件。...
    99+
    2023-10-26
    对象 文件 大数据
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作