Spring boot property binding Environment and Binder

Environment

  • Property binding method for springboot version 1. X.

  • It is suitable for the acquisition of simple properties and not suitable for the binding of complex objects.

  • Method:

      //Determine whether the key value is included
      boolean containsProperty(String key);
      
      //Get the property value, if not, return null
      String getProperty(String key);
      
      //Get the property value. If not, return the default value.
      String getProperty(String key, String defaultValue);
      
      //Gets the property object; its transformation is related to Converter, and finds the Converter according to sourceType and targetType
      <T> T getProperty(String key, Class<T> targetType);
    

Binder

  • The newly introduced class of Springboot 2.x deals with the binding between objects and multiple configurationpropertysources (properties).

  • It is much easier to use than Environment class, and it can be very convenient for type conversion, as well as providing callback methods to intervene in various stages of binding for deep customization.

      //Bound object
      MailPropertiesC propertiesC = Binder.get(environment) //First, bind the configurator
          //Then bind the property to the object
          .bind( "kaka.cream.mail-c", Bindable.of(MailPropertiesC.class) ).get(); //Get instance again
          
      //Binding Map
      Map<String,Object> propMap = Binder.get(environment)
          .bind( "fish.jdbc.datasource",Bindable.mapOf(String.class, Object.class) ).get();
          
      //Binding List
      List<String> list = Binder.get(environment)
          .bind( "kaka.cream.list",Bindable.listOf(String.class) ).get();
          
      //Conversion and default
      String datestr = (String) Binder.get(environment)
          .bind( "kaka.cream.date",Bindable.of(String.class) )
              //Map to uppercase
              .map(String::toUpperCase)
              /** 
              .map(new Function(){
                @Override
                public Object apply(Object o) {
                  String str = (String)o;
                  return str.toUpperCase();
                }
              })
              **/
              //Default value
              .orElse("bad date string");
               
      //Binding process callback function, highly customized
      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("Binding begins{}",name);
              return target;
            }
            
            @Override
            public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, 
                  BindContext context, Object result) {
              log.info("Binding success{}",target.getValue());
              return result;
            }
       
            @Override
            public Object onFailure(ConfigurationPropertyName name, Bindable<?> target, 
                  BindContext context, Exception error) throws Exception {
              log.info("Binding failed{}",name);
              return "No matching properties found";
            }
       
            @Override
            public void onFinish(ConfigurationPropertyName name, Bindable<?> target, 
                  BindContext context, Object result) throws Exception {
              log.info("End of binding{}",name);
            }
          }).get();
    
  • Reference articles

  • Code example: ideaprojects / dynamic switch datasource / dynamicdatasourceregister

Tags: SpringBoot JDBC

Posted on Sat, 02 Nov 2019 17:42:30 -0700 by stuart7398