AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

SpringBoot一個非常強大的數據綁定類

來源: 責編: 時間:2024-05-09 09:25:56 133觀看
導讀環境:SpringBoot3.2.51. 簡介本篇文章將介紹Spring Boot中一個非常強大且十分重要的類Binder,該類可以將外部配置文件的屬性值綁定到Spring Boot應用程序中的Java對象上。在Spring Boot中,通常使用@ConfigurationPropert

環境:SpringBoot3.2.5OOq28資訊網——每日最新資訊28at.com

1. 簡介

本篇文章將介紹Spring Boot中一個非常強大且十分重要的類Binder,該類可以將外部配置文件的屬性值綁定到Spring Boot應用程序中的Java對象上。在Spring Boot中,通常使用@ConfigurationProperties注解來指定外部配置文件中的屬性前綴,并使用Binder的bind方法將配置值綁定到Java對象上。這樣,Spring Boot應用程序可以方便地讀取和使用配置文件中的屬性配置。OOq28資訊網——每日最新資訊28at.com

2. 實戰案例

2.1 準備綁定對象

public class Person {  private Integer age ;  private String name ;  // getter, setter}

配置文件中添加配置屬性OOq28資訊網——每日最新資訊28at.com

pack:  person:   age: 20   name: 張三

測試綁定組件OOq28資訊網——每日最新資訊28at.com

@Componentpublic class BinderComponent implements InitializingBean {  private final Environment env ;  // 注入該對象是為了后面我們方便注冊自定義數據類型轉換  private final ConversionService conviersionService ;  public BinderComponent(Environment env,     ConversionService conviersionService) {    this.env = env ;    this.conviersionService = conviersionService ;  }  public void afterPropertiesSet() throws Exception {    // 綁定測試都將在這里完成  }}

后續案例都將基于上面的環境OOq28資訊網——每日最新資訊28at.com

2.2 基礎綁定

// 這里的pack.person是配置文件中的前綴BindResult<Person> result = Binder.get(env).bind("pack.person", Person.class) ;Person person = result.get() ;System.out.println(person) ;

在該示例中,配置文件中的age屬性能正確的轉換為Integer。為什么能進行數據類型轉換?因為內部(調用Binder#get(env)時)會添加TypeConverterConversionService和ApplicationConversionService兩個類型轉換器。OOq28資訊網——每日最新資訊28at.com

2.3 自定義數據類型轉換

給Person添加Date類型的字段,如下:OOq28資訊網——每日最新資訊28at.com

public class Person {  private Integer age ;  private String name ;  private Date birthday ;  // getter, setter}// 配置文件中添加birthday屬性pack:  person:    birthday: 2000-01-01

在此執行上面2.2中代碼,程序拋出了如下異常OOq28資訊網——每日最新資訊28at.com

圖片圖片OOq28資訊網——每日最新資訊28at.com

默認的數據類型轉換器是沒有String到Date轉換功能。我們需要添加自定義的類型轉換,如下自定義類型轉換器:OOq28資訊網——每日最新資訊28at.com

@Configurationpublic class DataTypeConvertConfig implements WebMvcConfigurer {  @Override  public void addFormatters(FormatterRegistry registry) {    registry.addConverter(new Converter<String, Date>() {      @Override      public Date convert(String source) {        try {          return new SimpleDateFormat("yyyy-MM-dd").parse(source) ;        } catch (ParseException e) {          throw new RuntimeException(e) ;        }      }    });  }}

修改數據綁定方式OOq28資訊網——每日最新資訊28at.com

Iterable<ConfigurationPropertySource> propertySources = ConfigurationPropertySources.get(env) ;// 不使用默認的類型轉換服務,使用自定義(還是自動配置的,只是添加了我們自定義的)Binder binder = new Binder(propertySources, null, conviersionService) ;Person result = binder.bindOrCreate("pack.person", Person.class) ;System.out.println(result) ;

這次成功輸出結果。OOq28資訊網——每日最新資訊28at.com

圖片OOq28資訊網——每日最新資訊28at.com

2.4 數據綁定回調

我們還可以為Binder執行綁定時,傳入回調句柄,這樣在數據綁定的各個階段都可以進行相應的處理,如下示例:OOq28資訊網——每日最新資訊28at.com

Iterable<ConfigurationPropertySource> propertySources = ConfigurationPropertySources.get(env) ;Binder binder = new Binder(propertySources, null, conviersionService) ;Person result = binder.bindOrCreate("pack.person", Bindable.of(Person.class), new BindHandler() {  @Override  public <T> Bindable<T> onStart(ConfigurationPropertyName name, Bindable<T> target, BindContext context) {    System.out.printf("準備進行數據綁定:【%s】%n", name) ;    return target ;  }  @Override  public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result) {    System.out.printf("對象綁定成功:【%s】%n", result) ;    return result ;  }  @Override  public Object onCreate(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result) {    System.out.printf("準備創建綁定對象:【%s】%n", result) ;    return result ;  }  @Override  public Object onFailure(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Exception error)      throws Exception {    System.out.printf("數據綁定失敗:【%s】%n", error.getMessage()) ;    return BindHandler.super.onFailure(name, target, context, error);  }  @Override  public void onFinish(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result)      throws Exception {    System.out.printf("數據綁定完成:【%s】%n", result) ;    BindHandler.super.onFinish(name, target, context, result) ;  }}) ;System.out.println(result) ;

輸出結果OOq28資訊網——每日最新資訊28at.com

圖片圖片OOq28資訊網——每日最新資訊28at.com

每個屬性在綁定時都會執行相應的回調方法。OOq28資訊網——每日最新資訊28at.com

3. 都用在哪里?

在SpringBoot環境中所有的數據綁定功能都是通過Binder進行。下面列出幾個非常重要的地方OOq28資訊網——每日最新資訊28at.com

3.1 SpringBoot啟動時綁定SpringApplication

SpringBoot在啟動時初始化環境配置Environment時,會將配置文件中的spring.main.*下的配置屬性綁定到當前的SpringApplication對象上。OOq28資訊網——每日最新資訊28at.com

public class SpringApplication {  public ConfigurableApplicationContext run(String... args) {    ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);  }  private ConfigurableEnvironment prepareEnvironment(...) {    // ...    bindToSpringApplication(environment);      }    protected void bindToSpringApplication(ConfigurableEnvironment environment) {    try {      Binder.get(environment).bind("spring.main", Bindable.ofInstance(this));    }  }}

spring.main有如下配置:OOq28資訊網——每日最新資訊28at.com

圖片圖片OOq28資訊網——每日最新資訊28at.com

3.2 綁定使用@ConfigurationProperties類

@ConfigurationProperties注解的類是通過BeanPostProcessor處理器執行綁定(不管是類上使用該注解,還是@Bean注解的方法都是通過該處理器進行綁定)。OOq28資訊網——每日最新資訊28at.com

public class ConfigurationPropertiesBindingPostProcessor {  // 該類是由SpringBoot自動配置  private ConfigurationPropertiesBinder binder;  // 實例化bean,執行初始化方法之前  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {    // 綁定;    bind(ConfigurationPropertiesBean.get(this.applicationContext, bean, beanName));    return bean;  }}

上面的ConfigurationPropertiesBean.get方法會處理當前bean實例是獨立的一個Bean對象且類上有@ConfigurationProperties注解,或者是當前的bean實例是通過@Bean定義且方法上有@ConfigurationProperties注解。不管是哪種定義的bean只要滿足條件,都會被包裝成ConfigurationPropertiesBean對象。接下來執行bind方法:OOq28資訊網——每日最新資訊28at.com

private void bind(ConfigurationPropertiesBean bean) {  try {    this.binder.bind(bean);  }}

執行綁定OOq28資訊網——每日最新資訊28at.com

class ConfigurationPropertiesBinder {  BindResult<?> bind(ConfigurationPropertiesBean propertiesBean) {    Bindable<?> target = propertiesBean.asBindTarget();    ConfigurationProperties annotation = propertiesBean.getAnnotation();    BindHandler bindHandler = getBindHandler(target, annotation);    return getBinder().bind(annotation.prefix(), target, bindHandler);  }}

以上就是@ConfigurationProperties注解的類或方法對象通過Binder綁定的原理。OOq28資訊網——每日最新資訊28at.com

3.3 SpringCloud Gateway綁定路由謂詞&過濾器OOq28資訊網——每日最新資訊28at.com

當一個路由請求過來時,會查詢相應的路由,而這個查找過程中就會通過路由的定義信息轉換為Route對象。以下是大致過程(詳細還需要自行閱讀源碼)OOq28資訊網——每日最新資訊28at.com

public class RoutePredicateHandlerMapping {  protected Mono<?> getHandlerInternal(ServerWebExchange exchange) {    return lookupRoute(exchange)... ;  }  protected Mono<Route> lookupRoute(...) {    // 查找路由    return this.routeLocator.getRoutes()... ;  }}public class RouteDefinitionRouteLocator {  public Flux<Route> getRoutes() {    // 將在yaml配置中定義的路由轉換為Route對象    Flux<Route> routes = this.routeDefinitionLocator.getRouteDefinitions().map(this::convertToRoute);  }  private Route convertToRoute(RouteDefinition routeDefinition) {    AsyncPredicate<ServerWebExchange> predicate = combinePredicates(routeDefinition);    // 獲取配置過濾器    List<GatewayFilter> gatewayFilters = getFilters(routeDefinition);    return ... ;  }  private List<GatewayFilter> getFilters(RouteDefinition routeDefinition) {    List<GatewayFilter> filters = new ArrayList<>();    if (!this.gatewayProperties.getDefaultFilters().isEmpty()) {      // loadGatewayFilters方法中進行配置的綁定      filters.addAll(loadGatewayFilters(routeDefinition.getId(),          new ArrayList<>(this.gatewayProperties.getDefaultFilters())));    }  }  List<GatewayFilter> loadGatewayFilters(...) {    Object configuration = this.configurationService.with(factory)      ...      // 該方法執行綁定動作      .bind();  }  public T bind() {    T bound = doBind();  }  protected T doBind() {    Bindable<T> bindable = Bindable.of(this.configurable.getConfigClass());    T bound = bindOrCreate(bindable, this.normalizedProperties, this.configurable.shortcutFieldPrefix(),        /* this.name, */this.service.validator.get(), this.service.conversionService.get());    return bound;  }}

以上源碼比較粗略,大家只要知道原理即可,沒必要任何一個點都搞的清清楚楚。OOq28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-87490-0.htmlSpringBoot一個非常強大的數據綁定類

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 使用Ollama和Go基于文本嵌入模型實現文本向量化

下一篇: RabbitMQ如何保證消息可靠性?

標簽:
  • 熱門焦點
  • Raft算法:保障分布式系統共識的穩健之道

    1. 什么是Raft算法?Raft 是英文”Reliable、Replicated、Redundant、And Fault-Tolerant”(“可靠、可復制、可冗余、可容錯”)的首字母縮寫。Raft算法是一種用于在分布式系統
  • Rust中的高吞吐量流處理

    作者 | Noz編譯 | 王瑞平本篇文章主要介紹了Rust中流處理的概念、方法和優化。作者不僅介紹了流處理的基本概念以及Rust中常用的流處理庫,還使用這些庫實現了一個流處理程序
  • CSS單標簽實現轉轉logo

    轉轉品牌升級后更新了全新的Logo,今天我們用純CSS來實現轉轉的新Logo,為了有一定的挑戰性,這里我們只使用一個標簽實現,將最大化的使用CSS能力完成Logo的繪制與動畫效果。新logo
  • SpringBoot中使用Cache提升接口性能詳解

    環境:springboot2.3.12.RELEASE + JSR107 + Ehcache + JPASpring 框架從 3.1 開始,對 Spring 應用程序提供了透明式添加緩存的支持。和事務支持一樣,抽象緩存允許一致地使用各
  • 簽約井川里予、何丹彤,單視頻點贊近千萬,MCN黑馬永恒文希快速崛起!

    來源:視聽觀察永恒文希傳媒作為一家MCN公司,說起它的名字來,可能大家會覺得有點兒陌生,但是說出來下面一串的名字之后,或許大家就會感到震驚,原來這么多網紅,都簽約這家公司了。根
  • 認真聊聊東方甄選:如何告別低垂的果實

    來源:山核桃作者:財經無忌爆火一年后,俞敏洪和他的東方甄選依舊是頗受外界關心的&ldquo;網紅&rdquo;。7月5日至9日,為期5天的東方甄選&ldquo;甘肅行&rdquo;首次在自有App內直播,
  • 華為發布HarmonyOS 4:更好玩、更流暢、更安全

    在8月4日的華為開發者大會2023(HDC.Together)大會上,HarmonyOS 4正式發布。自2019年發布以來,HarmonyOS一直以用戶為中心,經歷四年多的發展HarmonyOS已
  • 世界人工智能大會國際日開幕式活動在世博展覽館開啟

    30日上午,世界人工智能大會國際日開幕式活動在世博展覽館開啟,聚集國際城市代表、重量級院士專家、國際創新企業代表,共同打造人工智能交流平臺。上海市副市
  • 利用職權私自解除被封帳號 Meta開除20多名員工

    11月18日消息,據外媒援引知情人士表示,過去一年時間內,Facebook母公司Meta解雇或處罰了20多名員工以及合同工,指控這些人通過內部系統以不當方式重置用戶帳號,其
Top