springboot之@ConfigurationProperties的使用

编程

上面就是官方文档的解释,如果你想使用多个属性或者你的数据是由层次结构的,那么就可以使用@ConfigurationProperties来处理

那么什么是层次的结构呢,实际上就是我们开发过程中说的属性的嵌套,例如一个Person 类中有一个引用类型是Address 那么这种就是有层次结构的


那么怎么使用呢

Spring Boot provides infrastructure to bind @ConfigurationProperties types and register them as beans. You can either enable configuration properties on a class-by-class basis or enable configuration property scanning that works in a similar manner to component scanning.

大概意思是说spring-boot对绑定@ConfigurationProperties 提供了一个基础的功能,你可以通过扫描组件或者通过创建类的方式进行,那么常用的大概有这几种

1.@EnableConfigurationProperties 和ConfigurationProperties 配合使用

@ConfigurationProperties(prefix = "acme")

@Data

public class AcmeProperties {

private boolean enabled;

private InetAddress remoteAddress;

private final Security security = new Security();

@Data

public static class Security {

private String username;

private String password;

private List<String> roles = new ArrayList<>(Collections.singleton("USER"));

}

}

@Configuration

@EnableConfigurationProperties({AcmeProperties.class})

public class AcmeConfigTest {

}

@RestController

@RequestMapping("/properties-get")

public class PropertiesTestController {

//此处注入

@Autowired

private AcmeProperties acmeProperties;

@GetMapping("/address")

public String getRemoteAddress(){

return JSON.toJSONString(acmeProperties.getRemoteAddress());

}

@GetMapping("/enable")

public String getEnabled(){

return JSON.toJSONString(acmeProperties.isEnabled());

}

@GetMapping("/security")

public String getPropertyValue(){

return JSON.toJSONString(acmeProperties.getSecurity());

}

}

这种在写你自己的自动配置时经常用到,可以参考spring-boot 有关的自动配置大部分都是这么干的

2.@Component和@ConfigurationProperties配合

@ConfigurationProperties(prefix = "acme")

@Data

@Component

public class AcmeProperties {

private boolean enabled;

private InetAddress remoteAddress;

private final Security security = new Security();

@Data

public static class Security {

private String username;

private String password;

private List<String> roles = new ArrayList<>(Collections.singleton("USER"));

}

}

@RestController

@RequestMapping("/properties-get")

public class PropertiesTestController {

@Autowired

private AcmeProperties acmeProperties;

@GetMapping("/address")

public String getRemoteAddress(){

return JSON.toJSONString(acmeProperties.getRemoteAddress());

}

@GetMapping("/enable")

public String getEnabled(){

return JSON.toJSONString(acmeProperties.isEnabled());

}

@GetMapping("/security")

public String getPropertyValue(){

return JSON.toJSONString(acmeProperties.getSecurity());

}

}

3.使用在方法上和@Bean一起

@Data

public class AcmeProperties {

private boolean enabled;

private InetAddress remoteAddress;

private final Security security = new Security();

@Data

public static class Security {

private String username;

private String password;

private List<String> roles = new ArrayList<>(Collections.singleton("USER"));

}

}

@Configuration

public class AcmeConfigTest {

@ConfigurationProperties(prefix = "acme")

@Bean

public AcmeProperties acmeProperties(){

return new AcmeProperties();

}

}

这种方式,在绑定第三方配置特别有用

有关配置文件

acme.enabled = true

acme.remote-address=127.0.0.1

acme.security.username = "wei"

acme.security.roles = {"role1","role2"}

以上是 springboot之@ConfigurationProperties的使用 的全部内容, 来源链接: utcz.com/z/515112.html

回到顶部