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

网友投稿 294 2022-08-28


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

目录代码构造器绑定结合@PropertySource

代码

pom.xml:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.6.2

jar

com.kaven

springboot

0.0.1-SNAPSHOT

springboot

springboot

1.8

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.6.2

jar

com.kaven

springboot

0.0.1-SNAPSHOT

springboot

springboot

1.8

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

org.springframework.boot

spring-boot-maven-plugin

配置类:

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

private Map scores;

private List 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 hobbies;

private Map scores;

private List userToken;

public UserProperties(String username,

String password,

Set hobbies,

Map scores,

List userToken) {

this.username = username;

this.password = password;

this.hobbies = hobbies;

this.scores = scores;

this.userToken = userToken;

}

}

使用@ConstructorBinding注解修饰类的问题在于类中可能有多个构造器http://,如果出现这种情况就会有问题。

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.SeHJVBFwat;@ConfigurationProperties(prefix = "user")@ToString@ConstructorBindingpublic class UserProperties { private String username; private String password; private Set hobbies; private Map scores; private List userToken; public UserProperties() {} public UserProperties(String username, String password, Set hobbies, Map scores, List userToken) { 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 hobbies;

private Map scores;

private List userToken;

public UserProperties() {}

@ConstructorBinding

public UserProperties(String username,

String password,

Set hobbies,

Map scores,

List 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 {}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python-文件(读写)
下一篇:python3教程:reduce函数的用法(python3.x中reduce()是内置函数)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~