SpringBoot 配置 yaml,properties 配置文件值获取(数组,对象,Map)以及 properties 配置文件打印中文乱码问题解决

  • 将配置文件中配置的每一个属性的值,映射到这个组件中
  • @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
  • prefix = "person":配置文件中哪个下面的所有属性进行一一映射
  • 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能

Person:

package com.atguigu.springboot.pojo;

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

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

/**
 * @Classname Person
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String name;
    private Integer age;
    private Boolean stage;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Child child;

    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 Boolean getStage() {
        return stage;
    }

    public void setStage(Boolean stage) {
        this.stage = stage;
    }

    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 Child getChild() {
        return child;
    }

    public void setChild(Child child) {
        this.child = child;
    }

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

Child:

package com.atguigu.springboot.pojo;

/**
 * @Classname Child
 */
public class Child {
    private String name;
    private Integer 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;
    }

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

导入配置文件处理器,以后编写配置就有提示  

                <!--生成自己的配置元数据文件
		包含一个Java注释处理器,在编译项目时调用该处理器-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

application.yml

person:
   name: typ
   age: 20
   stage: true
   birth: 2019/07/15
   maps: {name: jack, age: 12}
   lists:
    - dog
    - cat
   child:
    name: lucy
    age: 11



测试:

package com.atguigu.springboot;

import com.atguigu.springboot.pojo.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;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot01HelloworldApplicationTests {

	@Autowired
	private Person person;
	@Test
	public void contextLoads() {
		System.out.println(person.toString());
	}

}

结果:

Person{name='typ', age=20, stage=true, birth=Mon Jul 15 00:00:00 CST 2019, maps={name=jack, age=12}, lists=[dog, cat], child=Child{name='lucy', age=11}}

 

application.properties

#person
person.name=詹金斯
person.age=20
person.birth=2019/01/01
person.stage=true
person.maps.name=jack
person.maps.age=16
person.lists=a,b,c,d,e,f
person.child.name=露西
person.child.age=11

结果(有乱码):

Person{name='ղ��˹', age=20, stage=true, birth=Tue Jan 01 00:00:00 CST 2019, maps={age=16, name=jack}, lists=[a, b, c, d, e, f], child=Child{name='¶��', age=11}}

 File-->Settings

重新输入中文:

测试结果:

Person{name='詹金斯', age=20, stage=true, birth=Tue Jan 01 00:00:00 CST 2019, maps={age=16, name=jack}, lists=[a, b, c, d, e, f], child=Child{name='露西', age=11}}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值