springboot中读取配置文件@Value和@Configuration

[email protected]

package com.xgcd.springboot.bean;

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

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

/**
 * 将配置文件中的配置映射到这个组件中
 *
 * @ConfigurationProperties 告诉springboot将本类中的所有属性和配置文件中的相关配置进行绑定:
 * prefix:配置文件中的哪个属性下的所有属性进行一一绑定
 * 只有该组件是容器中的组件才可以发挥作用(提供@ConfigurationProperties功能)
 * 可以在test中进行单元测试
 */
@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:application.properties"}, encoding = "utf-8")
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;

    @Override
    public String toString() {
        return "Person{" +
                "lastName=‘" + lastName + ‘\‘‘ +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                ‘}‘;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer 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, 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;
    }
}

[email protected]

二者比较

补充读取.properties文件乱码问题

原文地址:https://www.cnblogs.com/yadongliang/p/11617832.html

时间: 2024-08-29 10:53:58

springboot中读取配置文件@Value和@Configuration的相关文章

在ASP.NET 5中读取配置文件

(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 在ASP.NET 5中摒弃了之前配置文件的基础结构,引入了一个全新配置文件系统.今天推荐的文章就简单介绍了这方面的内容. 微软在即将正式发布的跨平台运行时当中引入了一个全新的配置文件基础结构,基于这个配置系统,从多个配置文件中读取内容从来没有这么容易过. 这个配置系统不仅内置支持json,xml和ini格式的配置文件,还支持从环境变量中读取内容.如下: var configuration = new

.NET Core在类库中读取配置文件appsettings.json

在.NET Framework框架时代我们的应用配置内容一般都是写在Web.config或者App.config文件中,读取这两个配置文件只需要引用System.Configuration程序集,分别用 System.Configuration.ConfigurationManager.AppSettings["SystemName"];//读取appSettings配置 System.Configuration.ConfigurationManager.ConnectionStrin

springboot中读取自定义properties文件

一.在高版本的springboot中,@ConfigurationProperties(prefix = "wisely2",locations = "classpath:wisely.properties")这个注解不支持了,所以我们要另辟蹊径 二.使用组合式注解: 1.自定义config.properties文件: 1 config.fileServer=/root/jzyp/staticserver/webapps/ROOT/server 2 config.s

Java中读取配置文件(properties、xml)

1. 利用java.util提供的工具类Properties - 首先我这边有个file.properties文件 - 然后去读取这个文件 注意项目路径 //注意ProConfigUtil是当前类,也可用this替代 InputStream inputStream = PropConfigUtil.class.getClassLoader() .getResourceAsStream("config/file.properties"); Properties pro = new Pro

在程序集或控制台程序中读取配置文件

https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?tabs=basicconfiguration Nuget 添加 Microsoft.Extensions.Configuration.Json Microsoft.Extensions.Options.ConfigurationExtensions using Microsoft.Extensions.Configuration; using Sys

java中读取配置文件ResourceBundle和Properties两种方式比较

今天在开发的时候,需要把一些信息放到配置文件中,方便后续的修改,注意到用的是ResourceBundle读取配置文件的方式,记得之前也见过使用Properties的方式,就比较好奇这两种方式的区别,网上查了一下和查了一下Java API手册,简单总结记录一下: ResourceBundle和Properties的一个主要区别就是ResourceBundle支持语言国际化,当程序需要特定于语言环境的对象时,它使用 getBundle 方法加载 ResourceBundle 类: Locale lo

Springboot中读取.yml文件

自定义配置文件application-dev.yml 1 spring: 2 dataresource: 3 druid: 4 driver-class-name: com.mysql.jdbc.Driver 5 url: jdbc:mysql://127.0.0.1:3306/appcloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC 6 username: root 7 password: root 创建一个实

关于springboot中的配置文件小知识

springboot中可以有多个配置文件,配置文件可以是.properties或则yml结尾的文件,并且配置文件有优先级,相同种类的配置application.properties 的优先级比较高,不同种类配置同时都会生效. 也可以自定义配置文件,随意命名,但是后缀名必须按照要求来!后台也可以通过@Value(“${key}”)注解来绑定值到属性: 原文地址:https://www.cnblogs.com/ljl5921/p/11801569.html

Springboot如何读取配置文件中的属性

Springboot自定义属性注入 SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用application.properties或者application.yml(application.yaml)进行配置. SpringBoot默认会从resources目录下加载application.properties或application.yml(application.yaml)文件 下面介绍如何获取配置文件中的属性 我们以自定义数据源为例