@ConfigurationProperties怎么在春天Boot2.0中使用

  介绍

本篇文章给大家分享的是有关@ConfigurationProperties怎么在春天Boot2.0中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

<强>配置项目POM

在POM。xml中定义Spring-Boot为父

& lt; parent>   & lt;才能groupId> org.springframework.boot</groupId>   & lt;才能artifactId> spring-boot-starter-parent</artifactId>   & lt;才能version> 2.0.4.RELEASE</version>   & lt;才能relativePath/祝辞,& lt; !——, lookup  parent 得到repository ——比;   ,& lt;/parent>

添加依赖

<李>

添加网络,因为我们需要使用jsr - 303到规范的验证器,如果不想使用web依赖,也可以直接依赖hibernate验证框架的

<李>

添加spring-boot-configuration-processor,可以在编译时生成属性元数据(spring-configuration-metadata.json)。李李

<>

添加lombok,可以方便使用注释处理器的功能省去Pojo定义中得到设置这些麻烦工作。

,, & lt; dependency>   ,,& lt; groupId> org.springframework.boot   ,,& lt; artifactId> spring-boot-starter-web   & lt;才能/dependency>   & lt;才能!——& lt; dependency>——比;   ,,& lt; !——& lt; groupId> org.hibernate.validator——比;   ,,& lt; !——& lt; artifactId> hibernate-validator——比;   ,,& lt; !——& lt; version> 6.0.11.Final——比;   ,,& lt; !——& lt; scope> compile——比;   & lt;才能!——& lt;/dependency>——比;   & lt;才能dependency>   ,,& lt; groupId> org.projectlombok   ,,& lt; artifactId> lombok   & lt;才能/dependency>   & lt;才能dependency>   ,,& lt; groupId> org.springframework.boot   ,,& lt; artifactId> spring-boot-configuration-processor   ,,& lt; optional> true   & lt;才能/dependency>

<强>例子编写

首先定义一个DocumentServerProperties对象、下面这个文档服务器配置是我假设的,主要是为了演示属性配置的大部分情况

@ getter   @ setter   public  class  DocumentServerProperties  {      private 才能;String  remoteAddress;   private 才能;boolean  preferIpAddress;   private 才能;int  maxConnections=0;   private 才能int 港口;   private 才能;AuthInfo  authInfo;   private 才能;List< String>,白名单;   private 才能;Map<字符串,String>,转换器;   private 才能;List< Person>, defaultShareUsers;      ,@ getter   ,@ setter   public 才能static  class  AuthInfo  {      ,,,private  String 用户名;   ,,,private  String 密码;   ,,}   }

<>强绑定属性配置

注意@ConfigurationProperties并没有把当前类注册成为一个春天的Bean,下面介绍@ConfigurationProperties配置注入的三种方式。

配合@ component注解直接进行注入

@ConfigurationProperties (=prefix “doc")   @ component   public  class  DocumentServerProperties  {//代才能码……   }

使用@EnableConfigurationProperties,通常配置在标有@ configuration的类上,当然其他@ component注解的派生类也可以,不过不推荐。

@ConfigurationProperties (=prefix “doc")   public  class  DocumentServerProperties  {//代才能码……   } @EnableConfigurationProperties   @ configuration   public  class  SomeConfiguration  {   private 才能;DocumentServerProperties  documentServerProperties   ,,,,   public 才能;SomeConfiguration (DocumentServerProperties  documentServerProperties), {   ,,,this.documentServerProperties =, documentServerProperties;   ,,}      }

使用@ bean方式在标有@ configuration的类进行注入,这种方式通常可以用在对第三方类进行配置属性注册

@ configuration   public  class  SomeConfiguration  {   ,,   ,@ bean   public 才能;DocumentServerProperties  documentServerProperties () {   ,,,return  new  DocumentServerProperties ();   ,,}   ,,   @ConfigurationProperties才能(“demo.third")   ,@ bean   public 才能;ThirdComponent  thirdComponent () {   ,,,return  new  ThirdComponent ();   ,,}      }

@ConfigurationProperties怎么在春天Boot2.0中使用