好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring Boot中的属性绑定的实现

之前翻译了一篇不怎么样的文章,主要是翻译的水平有限,自己翻译的云里雾里,发现平时只会有@configurationproperties注解,对springboot强大的属性绑定知之甚少,所以以那篇文章为线索,重新学习了一遍。

@configurationproperties

在使用的时候,我们往往只关心两件事,属性怎么绑定,即属性文件中的值和配置类中字段的映射关系;其次是类实例化的时机。故而衍生开来configurationproperties有三种用法。

@component + @configurationproperties

这种用法最简单,直接在pojo类上加上注解即可,spring容器初始化时就会生成配置类实例了。适合pojo类是自定义的。

?

1

2

3

4

5

6

@component

@configurationproperties (prefix = "kaka.cream.mail-a" ,ignoreunknownfields = false )

public class mailpropertiesa {

   private string name;

   private string sex;

   private integer age;

@bean + @configurationproperties

在配置类中进行装配,这两个注解均出现在configuration中,对pojo无侵入,使用灵活,且集中(均在配置类中处理)

?

1

2

3

4

5

6

@bean

   @configurationproperties (prefix = "kaka.cream.mail-b" ,ignoreunknownfields = false )

   public mailpropertiesb mailpropertiesb(){

     mailpropertiesb b = new mailpropertiesb();

     return b;

   }

@enableconfigurationproperties + @configurationproperties

pojo类上注解@configurationproperties,在启动类上注解@enableconfigurationproperties

?

1

2

3

4

5

6

7

@data

@configurationproperties (prefix = "kaka.cream.mail-c" ,ignoreunknownfields = false )

public class mailpropertiesc {

   private string name;

   private string sex;

   private integer age;

}

?

1

2

@enableconfigurationproperties (mailpropertiesc. class )

public class gomvcapplicationtests {

可以在启动类上一目了然的看到启动的配置,且不需要配置类,对第三方使用者比较友好,但是灵活性上没有第二种好。在这三种里面,推荐使用第二种方式。

environment

存在于spring boot首个版本的元老类,它继承自propertyresolver,通过它,我们能知道激活的配置文件,以及获取对应参数的值,结合上面第二种在配置类中一起用。较常用的主要有

?

1

2

3

4

5

6

7

8

//判断是否包含键值

boolean containsproperty(string key);

//获取属性值,如果获取不到返回null

string getproperty(string key);

//获取属性值,如果获取不到返回缺省值

string getproperty(string key, string defaultvalue);

//获取属性对象

<t> t getproperty(string key, class <t> targettype);

其中最后一个转换是和converter有关的,会依据sourcetype和targettype查找转换器,这个打算下一个章节进行分析,不在这里展开。所以environment适合简单属性值的获取,不知何复杂对象的绑定。

binder

binder是在spring boot2新引入的api,从字面就可以看出来,[主打]绑定,可以非常方便的进行类型转化,以及提供回调方法介入绑定的各个阶段进行深度定制,结合上面第二种在配置类中一起用。其主要的类有binder, bindresult和bindhandler. 比environment好用很多,必备好类。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

//绑定对象

mailpropertiesc propertiesc = binder.get(environment).bind( "kaka.cream.mail-c" , bindable.of(mailpropertiesc. class )).get();

//绑定map

map<string,object> propmap = binder.get(environment).bind( "fish.jdbc.datasource" ,bindable.mapof(string. class , object. class )).get();

//绑定列表

list<string> list = binder.get(environment).bind( "kaka.cream.list" ,bindable.listof(string. class )).get();

//转换以及默认值

string datestr = (string) binder.get(environment).bind( "kaka.cream.date" ,bindable.of(string. class ))

         .map(string::touppercase)

         /** .map(new function(){

           @override

           public object apply(object o) {

             string str = (string)o;

             return str.touppercase();

           }

         })**/

         .orelse( "bad date string" );

        

//绑定过程回调函数,高度定制

localdate str = binder.get(environment).bind( "kaka.cream.date" , bindable.of(localdate. class ), new bindhandler() {

 

       @override

       public <t> bindable<t> onstart(configurationpropertyname name, bindable<t> target,

                   bindcontext context) {

         log.info( "绑定开始{}" ,name);

         return target;

       }

       @override

       public object onsuccess(configurationpropertyname name, bindable<?> target, bindcontext context, object result) {

         log.info( "绑定成功{}" ,target.getvalue());

         return result;

       }

 

       @override

       public object onfailure(configurationpropertyname name, bindable<?> target, bindcontext context, exception error) throws exception {

         log.info( "绑定失败{}" ,name);

         return "没有找到匹配的属性" ;

       }

 

       @override

       public void onfinish(configurationpropertyname name, bindable<?> target, bindcontext context, object result) throws exception {

         log.info( "绑定结束{}" ,name);

       }

     }).get();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://segmentfault.com/a/1190000018793404

查看更多关于Spring Boot中的属性绑定的实现的详细内容...

  阅读:12次