欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot中@Value獲取值和@ConfigurationProperties獲取值用法及比較

 更新時(shí)間:2024年08月08日 09:41:48   作者:岳軒子  
在Spring Boot中,@Value注解是一個(gè)非常有用的特性,它允許我們將外部的配置注入到我們的Bean中,@ConfigurationProperties用于將配置文件中的屬性綁定到 Java Bean 上,本文介紹了@Value獲取值和@ConfigurationProperties獲取值用法及比較,需要的朋友可以參考下

1. 簡(jiǎn)介

1.1 @value

在Spring Boot中,@Value注解是一個(gè)非常有用的特性,它允許我們將外部的配置(如application.properties或application.yml文件中的屬性)注入到我們的Bean中。這對(duì)于讀取配置信息,如數(shù)據(jù)庫連接信息、服務(wù)地址等,非常有用。

基本用法

@Value注解可以應(yīng)用于字段、setter方法或配置方法上。它使用SpEL(Spring Expression Language)表達(dá)式來讀取配置值。

1.2 @ConfigurationProperties

@ConfigurationProperties 是 Spring Boot 提供的一個(gè)非常強(qiáng)大的注解,用于將配置文件中的屬性綁定到 Java Bean 上。與 @Value 注解相比,@ConfigurationProperties 提供了更豐富的特性,比如松散綁定(relaxed binding)、JSR-303 數(shù)據(jù)校驗(yàn)以及復(fù)雜的類型綁定等。

基本用法

  1. 定義一個(gè)配置類:首先,你需要定義一個(gè)配置類,并使用 @ConfigurationProperties 注解來指定配置的前綴。
  2. 啟用配置屬性綁定:默認(rèn)情況下,Spring Boot 會(huì)自動(dòng)掃描帶有 @ConfigurationProperties 注解的類,并將它們注冊(cè)為 Spring 應(yīng)用上下文中的 bean。但是,如果你想要精確地控制哪些配置類被注冊(cè),你可以在 @EnableConfigurationProperties 注解中指定它們。
  3. 在配置文件中設(shè)置屬性:在 application.properties 或 application.yml 文件中設(shè)置與配置類屬性相對(duì)應(yīng)的配置項(xiàng)。

2. 使用

2.1 @value的使用

首先創(chuàng)建springboot的項(xiàng)目

創(chuàng)建application.yml

person:
  name : 岳軒子
  sex : 雄
  age : 18
  birthday : 2002/2/31
  maps : { k1 : 20 , k2 : 21}
  lists : [小黃 , 小黑]
  dog:
    name : 旺財(cái)

創(chuàng)建Person.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

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

@Component
@Validated
public class Person {
    @Value("${person.name}")
    private String name;
    @Value("${person.sex}")
    private Character sex;
    @Value("${person.age}")
    private Integer age;
    @Value("${person.birthday}")
    private Date birthday;
    private Map<String, Integer> maps;
    private List<String> lists;
    private Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Character getSex() {
        return sex;
    }

    public void setSex(Character sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Map<String, Integer> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Integer> maps) {
        this.maps = maps;
    }

    public List<String> getLists() {
        return lists;
    }

    public void setLists(List<String> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", age=" + age +
                ", birthday=" + birthday +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

創(chuàng)建Dog類

package com.example.springbootdaily.model;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

@Component
@Validated
public class Dog {
    @Value("${person.dog.name}")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

寫一個(gè)測(cè)試類

import com.example.springbootdaily.model.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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringTest {
    @Autowired
    Person person;

    @Test
    public void print(){
        System.out.println(person);
    }
}

運(yùn)行結(jié)果:

Person{name='岳軒子', sex=雄, age=18, birthday=Sun Mar 03 00:00:00 CST 2002, maps=null, lists=null, dog=null}

2.2 @ConfigurationProperties的用法

創(chuàng)建Person2.java

package com.example.springbootdaily.model;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

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

@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person2 {
    private String name;
    private Character sex;
    private Integer age;
    private Date birthday;
    private Map<String, Integer> maps;
    private List<String> lists;
    private Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Character getSex() {
        return sex;
    }

    public void setSex(Character sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Map<String, Integer> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Integer> maps) {
        this.maps = maps;
    }

    public List<String> getLists() {
        return lists;
    }

    public void setLists(List<String> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", age=" + age +
                ", birthday=" + birthday +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

Dog類

package com.example.springbootdaily.model;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

@Component
@Validated
public class Dog {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

寫測(cè)試類

 @Test
 public void print3(){
     System.out.println(person2);
 }

運(yùn)行結(jié)果:

Person{name='岳軒子', sex=雄, age=18, birthday=Sun Mar 03 00:00:00 CST 2002, maps={k1=20, k2=21}, lists=[小黃, 小黑], dog=Dog{name='旺財(cái)'}}

3. 區(qū)別

3.1 松散綁定

@ConfigurationProperties 的松散綁定(relaxed binding)是 Spring Boot 提供的一個(gè)特性,它允許你在配置文件中使用不同的命名風(fēng)格(如駝峰命名、短橫線分隔等),而 Spring Boot 能夠自動(dòng)地將其映射到 Java Bean 的屬性上。這種特性使得配置文件的編寫更加靈活,同時(shí)也使得 Java Bean 的屬性命名更加符合 Java 的命名習(xí)慣。

松散綁定的工作原理:
當(dāng)你使用 @ConfigurationProperties 注解來綁定配置文件中的屬性時(shí),Spring Boot 會(huì)嘗試根據(jù)以下規(guī)則來匹配屬性名:

  1. 駝峰命名與短橫線分隔的互轉(zhuǎn):如果你的 Java Bean 屬性使用駝峰命名(如 myProperty),那么你可以在配置文件中使用短橫線分隔的形式(如 my-property)來設(shè)置這個(gè)屬性的值。Spring Boot 會(huì)自動(dòng)地將這兩種命名風(fēng)格進(jìn)行轉(zhuǎn)換。
  2. 忽略大小寫:在松散綁定中,大小寫通常會(huì)被忽略,但請(qǐng)注意,這取決于你使用的配置文件格式(如 YAML 是大小寫敏感的,而 properties 文件則不是)。然而,即使對(duì)于大小寫敏感的文件格式,Spring Boot 也會(huì)嘗試以智能的方式匹配屬性名。
  3. 環(huán)境變量:對(duì)于環(huán)境變量,松散綁定的規(guī)則也適用。通常,環(huán)境變量名使用大寫字母和下劃線(如 MY_PROPERTY),而 Java Bean 屬性則使用駝峰命名。Spring Boot 能夠處理這種差異。

例子

application.yml
這里的name中間加了一個(gè)線

person:
  na-me : 岳軒子
  sex : 雄
  age : 18
  birthday : 2002/2/31
  maps : { k1 : 20 , k2 : 21}
  lists : [小黃 , 小黑]
  dog:
    name : 旺財(cái)

仍然可以獲取
運(yùn)行結(jié)果:

Person{name='岳軒子', 

3.2 SpEL

SpEL(Spring Expression Language)是 Spring 框架中的一個(gè)功能強(qiáng)大的表達(dá)式語言,它支持在運(yùn)行時(shí)查詢和操作對(duì)象圖。SpEL 是一種類似于 JSP EL(JavaServer Pages Expression Language)但功能更強(qiáng)大的表達(dá)式語言,它用于在運(yùn)行時(shí)查詢和操作數(shù)據(jù)。

主要用途

Bean 屬性的動(dòng)態(tài)訪問:在 Spring 配置文件中,你可以使用 SpEL 來動(dòng)態(tài)地訪問和設(shè)置 Bean 的屬性。
注解中的屬性值:在 Spring 的注解中,你也可以使用 SpEL 來設(shè)置注解的屬性值。
XML 配置中的屬性值:在 Spring 的 XML 配置文件中,可以通過 標(biāo)簽的 value 或 ref 屬性結(jié)合 SpEL 來設(shè)置屬性值。
@Value 注解:在 Java 代碼中,可以使用 @Value 注解結(jié)合 SpEL 來注入配置值或計(jì)算結(jié)果。

特點(diǎn)

功能強(qiáng)大:支持基本運(yùn)算、關(guān)系運(yùn)算、邏輯運(yùn)算、正則表達(dá)式匹配、集合操作等。
易于使用:語法簡(jiǎn)潔,易于學(xué)習(xí)和使用。
集成性好:與 Spring 框架緊密結(jié)合,可以在 Spring 的各種場(chǎng)景中使用。

例子

運(yùn)行結(jié)果

age=20,

3.3 JSP303數(shù)據(jù)校驗(yàn)

先導(dǎo)入依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

這里是@Value,不支持

@Email
@Value("${person.name}")
private String name;

如果不是email的話,會(huì)報(bào)錯(cuò)

3.4 復(fù)雜類型封裝

前面已經(jīng)使用了,@Value不能封裝map,list和對(duì)象類型
但是@ConfigurationProperties可以

以上就是SpringBoot中@Value獲取值和@ConfigurationProperties獲取值用法及比較的詳細(xì)內(nèi)容,更多關(guān)于@Value和@ConfigurationProperties比較的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • MyBatis實(shí)現(xiàn)多表聯(lián)合查詢r(jià)esultType的返回值

    MyBatis實(shí)現(xiàn)多表聯(lián)合查詢r(jià)esultType的返回值

    這篇文章主要介紹了MyBatis多表聯(lián)合查詢r(jià)esultType的返回值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 詳細(xì)介紹MyBatis 3.4.0版本的功能

    詳細(xì)介紹MyBatis 3.4.0版本的功能

    這篇文章主要給大家介紹了關(guān)于MyBatis 3.4.0版本的功能,文中只列舉部分重要的內(nèi)容,詳細(xì)內(nèi)容看官方說明,需要的朋友可以參考借鑒,下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-06-06
  • springboot內(nèi)置tomcat調(diào)優(yōu)并發(fā)線程數(shù)解析

    springboot內(nèi)置tomcat調(diào)優(yōu)并發(fā)線程數(shù)解析

    這篇文章主要介紹了springboot內(nèi)置tomcat調(diào)優(yōu)并發(fā)線程數(shù)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java中GC與四種引用的關(guān)系詳解

    Java中GC與四種引用的關(guān)系詳解

    這篇文章主要介紹了Java中GC與四種引用的關(guān)系詳解,Java 中一共有 4 種類型的引用 : StrongReference、 SoftReference、 WeakReference 以及 PhantomReference這 4 種類型的引用與 GC 有著密切的關(guān)系, 讓我們逐一來看它們的定義和使用場(chǎng)景,需要的朋友可以參考下
    2023-09-09
  • SpringBoot+Shiro學(xué)習(xí)之密碼加密和登錄失敗次數(shù)限制示例

    SpringBoot+Shiro學(xué)習(xí)之密碼加密和登錄失敗次數(shù)限制示例

    本篇文章主要介紹了SpringBoot+Shiro學(xué)習(xí)之密碼加密和登錄失敗次數(shù)限制示例,可以限制登陸次數(shù),有興趣的同學(xué)可以了解一下。
    2017-03-03
  • Maven依賴中scope的含義

    Maven依賴中scope的含義

    本文主要介紹了Maven依賴中scope的含義,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Java設(shè)計(jì)模式—觀察者模式詳解

    Java設(shè)計(jì)模式—觀察者模式詳解

    這篇文章主要介紹了Java設(shè)計(jì)模式—觀察者模式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • SpringCloud中Eureka的配置及使用講解

    SpringCloud中Eureka的配置及使用講解

    Eureka?服務(wù)注冊(cè)中心,主要用于提供服務(wù)注冊(cè)功能,當(dāng)微服務(wù)啟動(dòng)時(shí),會(huì)將自己的服務(wù)注冊(cè)到?Eureka?Server,這篇文章主要介紹了SpringCloud中Eureka的配置及詳細(xì)使用,需要的朋友可以參考下
    2023-01-01
  • SpringBoot 靜態(tài)資源導(dǎo)入及首頁設(shè)置問題

    SpringBoot 靜態(tài)資源導(dǎo)入及首頁設(shè)置問題

    本節(jié)了解一下 SpringBoot 中 Web 開發(fā)的靜態(tài)資源導(dǎo)入和首頁設(shè)置,對(duì)應(yīng) SpringBoot-03-Web 項(xiàng)目,本節(jié)主要是從源碼的角度,研究了一下靜態(tài)資源導(dǎo)入和首頁設(shè)置的問題
    2021-09-09
  • JavaWeb實(shí)現(xiàn)郵件發(fā)送接收功能

    JavaWeb實(shí)現(xiàn)郵件發(fā)送接收功能

    這篇文章主要為大家詳細(xì)介紹了JavaWeb郵件發(fā)送接收功能的實(shí)現(xiàn),郵件發(fā)送和接收功能是非常常用的功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-12-12

最新評(píng)論