最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中@ConfigurationProperties實現(xiàn)配置自動綁定的方法

 更新時間:2022年02月16日 14:42:25   作者:ITKaven  
本文主要介紹了SpringBoot中@ConfigurationProperties實現(xiàn)配置自動綁定的方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

代碼

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
    </parent>

    <packaging>jar</packaging>

    <groupId>com.kaven</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>springboot</name>
    <description>springboot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

配置類:

package com.kaven.springboot.config;

import lombok.Setter;
import lombok.ToString;

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

import java.util.List;
import java.util.Map;
import java.util.Set;

@ConfigurationProperties(prefix = "user")
@Setter
@ToString
public class UserProperties {
    private String username;
    private String password;
    private Set<String> hobbies;
    private Map<String, Integer> scores;
    private List<UserToken> userToken;
}

UserToken類:

package com.kaven.springboot.config;

import lombok.Setter;
import lombok.ToString;

@Setter
@ToString
public class UserToken {
    private String token;
}

接口:

package com.kaven.springboot.controller;

import com.kaven.springboot.config.UserProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ConfigController {
    @Resource
    private UserProperties userProperties;

    @GetMapping("/config")
    public String getConfig() {
        return userProperties.toString();
    }
}

application.properties

user.username="kaven"
user.password="itkaven"
user.hobbies[0]="A"
user.hobbies[1]="B"
user.hobbies[2]="C"
user.scores.mathematics=145
user.scores.english=80
user.user-token[0].token="A"
user.user-token[1].token="B"
user.user-token[2].token="C"

啟動類:

package com.kaven.springboot;

import com.kaven.springboot.config.UserProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties(value = {UserProperties.class})
//@ConfigurationPropertiesScan(basePackageClasses = {UserProperties.class})
public class SpringbootApplication {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(SpringbootApplication.class);
        application.run(args);
    }
}

下面這兩個注解都可以使得Spring Boot基于被@ConfigurationProperties注解修飾的類創(chuàng)建bean,因此UserProperties實例可以自動注入到控制器中。

@EnableConfigurationProperties(value = {UserProperties.class})
@ConfigurationPropertiesScan(basePackageClasses = {UserProperties.class})

@ConfigurationPropertiesScan注解還可以指定要被掃描的包數(shù)組。

@ConfigurationPropertiesScan(basePackages = {"com.kaven.springboot.config"})

啟動應用,訪問http://localhost:8080/config。

在這里插入圖片描述

效果符合預期。

構(gòu)造器綁定

Spring Boot將配置文件中的配置自動綁定到配置類,無非就是通過反射等手段,創(chuàng)建配置類實例,而配置項需要綁定到配置類實例的屬性,這一般通過屬性的set方法或者構(gòu)造器來實現(xiàn),上面的演示是通過set方法來進行綁定,這是@Setter注解的效果。

@Setter

如果需要通過構(gòu)造器將配置項綁定到配置類實例的屬性,可以使用@ConstructorBinding注解。

package com.kaven.springboot.config;

import lombok.ToString;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;

import java.util.List;
import java.util.Map;
import java.util.Set;

@ConfigurationProperties(prefix = "user")
@ToString
@ConstructorBinding
public class UserProperties {
    private String username;
    private String password;
    private Set<String> hobbies;
    private Map<String, Integer> scores;
    private List<UserToken> userToken;

    public UserProperties(String username,
                          String password,
                          Set<String> hobbies,
                          Map<String, Integer> scores,
                          List<UserToken> userToken) {
        this.username = username;
        this.password = password;
        this.hobbies = hobbies;
        this.scores = scores;
        this.userToken = userToken;
    }
}

使用@ConstructorBinding注解修飾類的問題在于類中可能有多個構(gòu)造器,如果出現(xiàn)這種情況就會有問題。

package com.kaven.springboot.config;import lombok.ToString;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.context.properties.ConstructorBinding;import java.util.List;import java.util.Map;import java.util.Set;@ConfigurationProperties(prefix = "user")@ToString@ConstructorBindingpublic class UserProperties {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E--> private String username; private String password; private Set<String> hobbies; private Map<String, Integer> scores; private List<UserToken> userToken; public UserProperties() {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->} public UserProperties(String username, String password, Set<String> hobbies, Map<String, Integer> scores, List<UserToken> userToken) {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E--> this.username = username; this.password = password; this.hobbies = hobbies; this.scores = scores; this.userToken = userToken; }}

因為Spring Boot不知道調(diào)用哪個構(gòu)造器。

在這里插入圖片描述

可以將@ConstructorBinding注解修飾在構(gòu)造器上。

package com.kaven.springboot.config;

import lombok.ToString;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;

import java.util.List;
import java.util.Map;
import java.util.Set;

@ConfigurationProperties(prefix = "user")
@ToString
public class UserProperties {
    private String username;
    private String password;
    private Set<String> hobbies;
    private Map<String, Integer> scores;
    private List<UserToken> userToken;

    public UserProperties() {}

    @ConstructorBinding
    public UserProperties(String username,
                          String password,
                          Set<String> hobbies,
                          Map<String, Integer> scores,
                          List<UserToken> userToken) {
        this.username = username;
        this.password = password;
        this.hobbies = hobbies;
        this.scores = scores;
        this.userToken = userToken;
    }
}

在這里插入圖片描述

結(jié)合@PropertySource

SourceConfig類:

package com.kaven.springboot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = "classpath:/static/user.properties")
public class SourceConfig {}

在這里插入圖片描述

在這里插入圖片描述

效果符合預期,@ConfigurationProperties實現(xiàn)配置自動綁定就介紹到這里,,更多相關(guān)SpringBoot @ConfigurationProperties 自動綁定內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java實現(xiàn)去除ArrayList重復字符串

    java實現(xiàn)去除ArrayList重復字符串

    本文主要介紹了java實現(xiàn)去除ArrayList重復字符串,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-09-09
  • IDEA 單元測試報錯:Class not found:xxxx springboot的解決

    IDEA 單元測試報錯:Class not found:xxxx springb

    這篇文章主要介紹了IDEA 單元測試報錯:Class not found:xxxx springboot的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 解析Java編程之Synchronized鎖住的對象

    解析Java編程之Synchronized鎖住的對象

    這篇文章主要介紹了解析Java編程之Synchronized鎖住的對象,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • Sparsearray稀疏數(shù)組原理及實例詳解

    Sparsearray稀疏數(shù)組原理及實例詳解

    這篇文章主要介紹了Sparsearray稀疏數(shù)組原理及實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • Java 反射機制詳解及實例代碼

    Java 反射機制詳解及實例代碼

    本文主要介紹Java 反射機制的知識,這里提供示例代碼幫助大家學習理解此部分知識,有需要的小伙伴可以參考下
    2016-09-09
  • Netty網(wǎng)絡編程實戰(zhàn)之搭建Netty服務器

    Netty網(wǎng)絡編程實戰(zhàn)之搭建Netty服務器

    Netty是JBOSS開源的一款NIO網(wǎng)絡編程框架,可用于快速開發(fā)網(wǎng)絡的應用。Netty是一個異步的、基于事件驅(qū)動的網(wǎng)絡應用框架,用于快速開發(fā)高性能的服務端和客戶端。本文將詳細說說如何搭建Netty服務器,需要的可以參考一下
    2022-10-10
  • Spring?Cloud實現(xiàn)遠程調(diào)用OpenFeign組件的方法

    Spring?Cloud實現(xiàn)遠程調(diào)用OpenFeign組件的方法

    本文主要介紹了SpringCloud中實現(xiàn)遠程調(diào)用的組件OpenFeign的相關(guān)知識與操作,包括OpenFeign的介紹、快速上手、參數(shù)傳遞和最佳實踐,感興趣的朋友跟隨小編一起看看吧
    2026-02-02
  • Springboot整合quartz實現(xiàn)多個定時任務實例

    Springboot整合quartz實現(xiàn)多個定時任務實例

    這篇文章主要介紹了Springboot整合quartz實現(xiàn)多個定時任務代碼實例,Quartz?是一款功能強大的開源任務調(diào)度框架,幾乎可以集成到任何?Java?應用程序中,Quartz?可用于創(chuàng)建簡單或復雜的任務調(diào)度,用以執(zhí)行數(shù)以萬計的任務,需要的朋友可以參考下
    2023-08-08
  • Java中的分布式鎖與同步鎖使用詳解

    Java中的分布式鎖與同步鎖使用詳解

    這篇文章主要介紹了Java中的分布式鎖與同步鎖使用詳解,在分布式系統(tǒng)中,由于存在多個節(jié)點并行執(zhí)行任務,可能會出現(xiàn)競爭條件和數(shù)據(jù)不一致的問題,分布式鎖通過約束同一時刻只有一個節(jié)點能夠獲得鎖的方式,確保了對共享資源的獨占訪問,需要的朋友可以參考下
    2023-07-07
  • java引用jpython的方法示例

    java引用jpython的方法示例

    這篇文章主要介紹了java引用jpython的方法,結(jié)合實例形式分析了java引用jpython及相關(guān)使用技巧,需要的朋友可以參考下
    2016-11-11

最新評論

大丰市| 延安市| 辽宁省| 台江县| 苍南县| 山阴县| 宝鸡市| 来凤县| 磐石市| 绥阳县| 虞城县| 西宁市| 青田县| 满城县| 黄梅县| 朝阳区| 濉溪县| 渭南市| 大城县| 政和县| 南岸区| 塔城市| 蒙阴县| 新龙县| 同德县| 安庆市| 衡东县| 雅江县| 潼关县| 沿河| 西丰县| 武宁县| 阿瓦提县| 海阳市| 南部县| 临夏县| 西乡县| 台北县| 顺义区| 花莲市| 汶川县|