亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot配置文件注入值的簡單實(shí)現(xiàn)

 更新時(shí)間:2025年11月05日 09:18:21   作者:222you  
本文主要介紹了SpringBoot配置文件注入值的簡單實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.yaml語法

k:(空格)v:表示一對鍵值對(空格必須有);

以空格的縮進(jìn)來控制層級關(guān)系;只要是左對齊的一列數(shù)據(jù),都是同一個(gè)層級的

例如:

server: 
  port: 8080
  path: /hello

值的寫法:

字面量:普通的值(數(shù)字,字符串,布爾)

k: v:字面直接來寫;

字符串默認(rèn)不用加上單引號或者雙引號;

"":雙引號;不會轉(zhuǎn)義字符串里面的特殊字符;特殊字符會作為本身想表示的意思

name: "zhangsan \n lisi":輸出;zhangsan 換行 lisi

'':單引號;會轉(zhuǎn)義特殊字符,特殊字符最終只是一個(gè)普通的字符串?dāng)?shù)據(jù)

name: ‘zhangsan \n lisi’:輸出;zhangsan \n lisi

對象、Map(屬性和值)(鍵值對):

k: v:在下一行來寫對象的屬性和值的關(guān)系;注意縮進(jìn)

對象還是k: v的方式

friends: 
  lastName: zhangsan
  age: 20

行內(nèi)寫法:

friends: {lastName: zhangsan,age: 18}

數(shù)組(List,set):

用 - 值表示數(shù)字當(dāng)中的一個(gè)元素

pets: 
  - cat
  - dog
  - pig

行內(nèi)寫法:

pets: [cat,dog,pig]

2.配置文件注入

1.@ConfigurationProperties注解注入

我們嘗試把配置文件當(dāng)中的值注入到bean里面:

實(shí)體類:

package com.qcby.domain;

import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;


/**
  *把配置文件里的每一個(gè)值映射到這組件中
  *@ConfigurationProperties:告訴Spring把這個(gè)類中的所有屬性和配置文件中的配置綁定
  *perfix="person" 配置文件中person的所有屬性進(jìn)行一一映射
  */
@Component
@ConfigurationProperties(prefix = "person")
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;

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

    public String getBirthString() {
        return birthString;
    }

    public void setBirthString(String birthString) {
        this.birthString = birthString;
    }

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

配置文件:

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

在Controller中返回這個(gè)對象:

    @Autowired
    private Person person;

    @GetMapping("/person")
    @ResponseBody
    public Person selectPerson(){
        return person;
    }

然后運(yùn)行啟動類

2.@Value注解注入

package com.qcby.domain;

import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.*;

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

    //直接賦值
    @Value("Hello")
    private String lastName;

    @Value("18")
    private Integer age;

    @Value("false")
    private Boolean boss;

    //Date類型用Value需要轉(zhuǎn)換
    @Value("2017/12/12")
    private String birthString;
    private Date birth;


    //要手動初始化(創(chuàng)建對象)
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;


    // 使用 @PostConstruct 在 Bean 初始化后處理復(fù)雜類型
    @PostConstruct
    public void init() {
        //處理 Date 類型
        try {
            this.birth = new Date(birthString);
        } catch (Exception e) {
            this.birth = new Date(); // 默認(rèn)當(dāng)前時(shí)間
        }

        // 初始化 Map
        this.maps = new HashMap<>();
        maps.put("k1", "v1");
        maps.put("k2", "v2");

        //初始化 List
        this.lists = Arrays.asList("lisi", "zhaoliu");

        //創(chuàng)建 Dog 對象
        this.dog = new Dog();
        dog.setName("小狗112312");
        dog.setAge(12);
    }

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

    public String getBirthString() {
        return birthString;
    }

    public void setBirthString(String birthString) {
        this.birthString = birthString;
    }

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

@Value后面的括號里的值可以直接對這些屬性賦值:
1513008000000 是一個(gè)時(shí)間戳,表示從 1970年1月1日 00:00:00 UTC 到指定時(shí)間的毫秒數(shù)

value也可以使用占位符來獲取配置文件里面的值:

package com.qcby.domain;

import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;

@Component
public class Person {


    @Value("${person.lastName:Hello1231}")
    private String lastName;

    @Value("${person.age:183}")
    private Integer age;

    @Value("${person.boss:true}")
    private Boolean boss;

    @Value("${person.birth:2016/12/31}")
    //Date類型用Value需要轉(zhuǎn)換
    private String birthString;
    private Date birth;


    //要手動初始化(創(chuàng)建對象)
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;


    // 使用 @PostConstruct 在 Bean 初始化后處理復(fù)雜類型
    @PostConstruct
    public void init() {
        //處理 Date 類型
        try {
            this.birth = new Date(birthString);
        } catch (Exception e) {
            this.birth = new Date(); // 默認(rèn)當(dāng)前時(shí)間
        }

        // 初始化 Map
        this.maps = new HashMap<>();
        maps.put("k1", "v1");
        maps.put("k2", "v2");

        //初始化 List
        this.lists = Arrays.asList("lisi", "zhaoliu");

        //創(chuàng)建 Dog 對象
        this.dog = new Dog();
        dog.setName("小狗112312");
        dog.setAge(12);
    }

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

    public String getBirthString() {
        return birthString;
    }

    public void setBirthString(String birthString) {
        this.birthString = birthString;
    }

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

區(qū)別:

3.@PropertySource

@PropertySource可以加載指定的配置文件

package com.qcby.domain;

import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;

@PropertySource("classpath:person.properties")
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String lastName;
    private Integer age;
    private Boolean boss;
    private String birthString;
    private Date birth;


    private Map<String,Object> maps;
    private List<Object> lists;
    private 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;
    }

    public String getBirthString() {
        return birthString;
    }

    public void setBirthString(String birthString) {
        this.birthString = birthString;
    }

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

想讓person.properties生效的話,要先把a(bǔ)pplication.yaml文件中有關(guān)person的配置去掉,因?yàn)閍pplication.yaml讀取優(yōu)先級更高,高優(yōu)先級的配置會覆蓋低優(yōu)先級的配置

person.lastName=1222222222222222222222222222
person.age=${random.int(10)}
person.boss=true
person.birthString=2017/12/10
person.birth=2017/12/10
person.maps.k1=v1
person.maps.k2=v2
person.lists[0]=lisi
person.lists[1]=zhaoliu
person.dog.name=大狗
person.dog.age=12

3.配置類注入

想讓Spring的配置文件生效,我們可以在配置類上使用@ImportResource注解

package com.qcby.config;

import com.qcby.domain.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//指明是一個(gè)配置類,代替之前的Spring配置文件
@Configuration
public class MyConfiguration {

    @Bean
    public HelloService helloService(){
        System.out.println("配置類給容器添加組件了.......");
        return new HelloService();
    }
}

到此這篇關(guān)于SpringBoot配置文件注入值的簡單實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Springboot配置文件值注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)動態(tài)管理示例

    SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)動態(tài)管理示例

    這篇文章主要為大家介紹了SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)動態(tài)管理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Spring Boot 集成 MongoDB Template 的步驟詳解

    Spring Boot 集成 MongoDB Template 的步驟

    MongoDB 是一個(gè)流行的 NoSQL 數(shù)據(jù)庫,適合處理大量非結(jié)構(gòu)化數(shù)據(jù),本篇文章將詳細(xì)介紹如何在 Spring Boot 3.4.0 中集成 MongoDB Template,從零開始構(gòu)建一個(gè)簡單的應(yīng)用程序,感興趣的朋友一起看看吧
    2024-12-12
  • IDEA下Servlet可能出現(xiàn)404的一些情況

    IDEA下Servlet可能出現(xiàn)404的一些情況

    相信有很多小伙伴遇到報(bào)錯(cuò)都不知道怎么處理,今天特地整理了這篇文章,文中對IDEA下Servlet可能出現(xiàn)404的一些情況作了詳細(xì)的介紹,需要的朋友可以參考下
    2021-06-06
  • Java實(shí)例化的幾種方法總結(jié)

    Java實(shí)例化的幾種方法總結(jié)

    這篇文章主要介紹了Java實(shí)例化的幾種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 深入解析Spring?Boot?的SPI機(jī)制詳情

    深入解析Spring?Boot?的SPI機(jī)制詳情

    這篇文章主要介紹了深入解析Spring?Boot的SPI機(jī)制詳情,SPI是JDK內(nèi)置的一種服務(wù)提供發(fā)現(xiàn)機(jī)制,可以用來啟用框架擴(kuò)展和替換組件,主要用于框架中開發(fā),更多相關(guān)介紹,感興趣的小伙伴可以參考一下下面文章內(nèi)容
    2022-08-08
  • java讀取其他服務(wù)接口返回的json數(shù)據(jù)示例代碼

    java讀取其他服務(wù)接口返回的json數(shù)據(jù)示例代碼

    這篇文章主要給大家介紹了關(guān)于java讀取其他服務(wù)接口返回的json數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-03-03
  • 詳解微信開發(fā)之a(chǎn)ccess_token之坑

    詳解微信開發(fā)之a(chǎn)ccess_token之坑

    access_token分類一是普通access_token,二是網(wǎng)頁授權(quán)access_token。這篇文章主要介紹了詳解微信開發(fā)之a(chǎn)ccess_token之坑,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • JDK8時(shí)間相關(guān)類超詳細(xì)總結(jié)(含多個(gè)實(shí)例)

    JDK8時(shí)間相關(guān)類超詳細(xì)總結(jié)(含多個(gè)實(shí)例)

    jdk1.8的一些新特性簡化了代碼的寫法,減少了部分開發(fā)量,下面這篇文章主要給大家介紹了關(guān)于JDK8時(shí)間相關(guān)類超詳細(xì)總結(jié),文中包含了多個(gè)實(shí)例代碼,需要的朋友可以參考下
    2023-01-01
  • 最新hadoop安裝教程及hadoop的命令使用(親測可用)

    最新hadoop安裝教程及hadoop的命令使用(親測可用)

    這篇文章主要介紹了最新hadoop安裝教程(親測可用),本文主要講解了如何安裝hadoop、使用hadoop的命令及遇到的問題解決,需要的朋友可以參考下
    2022-06-06
  • JVM系列之String.intern的性能解析

    JVM系列之String.intern的性能解析

    這篇文章主要介紹了JVM系列之String.intern的性能解析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06

最新評論