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

4-1 YAML配置文件 注入 JavaBean中

时间:2021-07-28 21:34:46      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:Fix   style   oid   com   pack   ati   aml   yml   factory   

新建 javaBean,有那么多个成员变量:【get、set、toString 那些不用我教了吧......】

     private String lastName;
        private Integer age;
        private Boolean boss;
        private Date birth;
        private Map<String,Object> maps;
        private List<Object> lists;
        private Dog dog;  //dog里面一个name(String) 一个age(Integer) 
技术图片
package com.bihu.Bean;

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



public class JavaBean {

        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 getBoss() {
            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;
        }
    }
View Code

 

然后呢 我们新建一个application.yml:

然后吧这些对着这些值写入:

person:
    lastName: hello
    age: 18
    boss: false
    birth: 2017/12/12
    maps: {k1: v1,k2: 12}
    lists:
     ‐ lisi
     ‐ zhaoliu
    dog:
      name: 小狗
      age: 12

 

怎么把yml配置文件的数据映射到JavaBean,我们在JavaBean中添加注解,但是要一个gav:

一定要这个gav 才能映射:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

然后你在JavaBean类添加以下注解(注意看注释 注释说明一切)

技术图片
package com.bihu.Bean;

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

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

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

        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 getBoss() {
            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;
        }
    }
View Code

条件:

1.添加到SpringBoot 成为组件,用@Component 即可,

2.映射绑定:用@ConfigurationProperties绑定,其中用属性 prefix 说明 绑定那个对象


 

 

最后我们测试一下,在SpringBoot下面的Test测试(自动生成的)

技术图片

 

 

 

按照下面我代码来即可:

package com.bihu;

import com.bihu.Bean.JavaBean;
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;

@RunWith(SpringRunner.class)    //有了@RunWith(SpringRunner.class)这些类才能实例化到spring容器中,自动注入才能生效,
@SpringBootTest
public class ApplicationTests {


    //这里测试JavaBean
    @Autowired
    JavaBean Person;

    @Test
    public void contextLoads() {
        System.out.println(Person);
    }

}

 

这里测试了 JavaBean,我们还打印了,结果如下:

Person{lastName=‘hello‘, age=18, boss=false, birth=Tue Dec 12 00:00:00 CST 2017, maps={k1=v1, k2=12}, lists=[‐ lisi ‐ zhaoliu], dog=Dog{name=‘小狗‘, age=12}}

可以看到 完全注入进去了

 

这就是YAML配置文件 注入 JavaBean中、

4-1 YAML配置文件 注入 JavaBean中

标签:Fix   style   oid   com   pack   ati   aml   yml   factory   

原文地址:https://www.cnblogs.com/bi-hu/p/15070067.html

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