碼迷,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
迷上了代碼!
4399在线看MV_久久99精品久久久久久久久久_成人又黄又爽又刺激视频_能收黄台的app不收费