码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-10-02 18:29:45      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:etag   问题   告诉   ret   last   bean   pac   乱码   补充   

1.@Configuration

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;
    }
}

技术图片

 

 

2.@Value

技术图片

 

 

 技术图片

二者比较

技术图片

 

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

 

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

标签:etag   问题   告诉   ret   last   bean   pac   乱码   补充   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!