返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot中@ConfigurationProperties实现配置自动绑定的方法
  • 504
分享到

SpringBoot中@ConfigurationProperties实现配置自动绑定的方法

2024-04-02 19:04:59 504人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录代码构造器绑定结合@PropertySource代码 pom.xml: <?xml version="1.0" encoding="UTF-8"?> <proj

代码

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注解修饰的类创建bean,因此UserProperties实例可以自动注入到控制器中。

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

@ConfigurationPropertiesScan注解还可以指定要被扫描的包数组

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

启动应用,访问http://localhost:8080/config

在这里插入图片描述

效果符合预期。

构造器绑定

Spring Boot将配置文件中的配置自动绑定到配置类,无非就是通过反射等手段,创建配置类实例,而配置项需要绑定到配置类实例的属性,这一般通过属性的set方法或者构造器来实现,上面的演示是通过set方法来进行绑定,这是@Setter注解的效果。

@Setter

如果需要通过构造器将配置项绑定到配置类实例的属性,可以使用@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注解修饰类的问题在于类中可能有多个构造器,如果出现这种情况就会有问题。

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不知道调用哪个构造器。

在这里插入图片描述

可以将@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
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;
    }
}

在这里插入图片描述

结合@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实现配置自动绑定就介绍到这里,,更多相关SpringBoot @ConfigurationProperties 自动绑定内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot中@ConfigurationProperties实现配置自动绑定的方法

本文链接: https://lsjlt.com/news/139423.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作