springboot的Customizer源碼解析
更新時間:2023年08月22日 09:56:42 作者:codecraft
這篇文章主要為大家介紹了springboot的Customizer源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
序
本文主要研究一下springboot的Customizer
TaskExecutorCustomizer
@FunctionalInterface
public interface TaskExecutorCustomizer {
/**
* Callback to customize a {@link ThreadPoolTaskExecutor} instance.
* @param taskExecutor the task executor to customize
*/
void customize(ThreadPoolTaskExecutor taskExecutor);
}之后再構造的時候通過ObjectProvider獲取即可
@Bean
@ConditionalOnMissingBean
public TaskExecutorBuilder taskExecutorBuilder(TaskExecutionProperties properties,
ObjectProvider<TaskExecutorCustomizer> taskExecutorCustomizers,
ObjectProvider<TaskDecorator> taskDecorator) {
TaskExecutionProperties.Pool pool = properties.getPool();
TaskExecutorBuilder builder = new TaskExecutorBuilder();
builder = builder.queueCapacity(pool.getQueueCapacity());
builder = builder.corePoolSize(pool.getCoreSize());
builder = builder.maxPoolSize(pool.getMaxSize());
builder = builder.allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout());
builder = builder.keepAlive(pool.getKeepAlive());
Shutdown shutdown = properties.getShutdown();
builder = builder.awaitTermination(shutdown.isAwaitTermination());
builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
builder = builder.customizers(taskExecutorCustomizers.orderedStream()::iterator);
builder = builder.taskDecorator(taskDecorator.getIfUnique());
return builder;
}
/**
* Set the {@link TaskExecutorCustomizer TaskExecutorCustomizers} that should be
* applied to the {@link ThreadPoolTaskExecutor}. Customizers are applied in the order
* that they were added after builder configuration has been applied. Setting this
* value will replace any previously configured customizers.
* @param customizers the customizers to set
* @return a new builder instance
* @see #additionalCustomizers(TaskExecutorCustomizer...)
*/
public TaskExecutorBuilder customizers(TaskExecutorCustomizer... customizers) {
Assert.notNull(customizers, "Customizers must not be null");
return customizers(Arrays.asList(customizers));
}TaskSchedulerCustomizer
@FunctionalInterface
public interface TaskSchedulerCustomizer {
/**
* Callback to customize a {@link ThreadPoolTaskScheduler} instance.
* @param taskScheduler the task scheduler to customize
*/
void customize(ThreadPoolTaskScheduler taskScheduler);
}
@Bean
@ConditionalOnMissingBean
public TaskSchedulerBuilder taskSchedulerBuilder(TaskSchedulingProperties properties,
ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
TaskSchedulerBuilder builder = new TaskSchedulerBuilder();
builder = builder.poolSize(properties.getPool().getSize());
Shutdown shutdown = properties.getShutdown();
builder = builder.awaitTermination(shutdown.isAwaitTermination());
builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
builder = builder.customizers(taskSchedulerCustomizers);
return builder;
}
/**
* Set the {@link TaskSchedulerCustomizer TaskSchedulerCustomizers} that should be
* applied to the {@link ThreadPoolTaskScheduler}. Customizers are applied in the
* order that they were added after builder configuration has been applied. Setting
* this value will replace any previously configured customizers.
* @param customizers the customizers to set
* @return a new builder instance
* @see #additionalCustomizers(TaskSchedulerCustomizer...)
*/
public TaskSchedulerBuilder customizers(TaskSchedulerCustomizer... customizers) {
Assert.notNull(customizers, "Customizers must not be null");
return customizers(Arrays.asList(customizers));
}RestTemplateCustomizer
@FunctionalInterface
public interface RestTemplateCustomizer {
/**
* Callback to customize a {@link RestTemplate} instance.
* @param restTemplate the template to customize
*/
void customize(RestTemplate restTemplate);
}
@Bean
@Lazy
@ConditionalOnMissingBean
public RestTemplateBuilderConfigurer restTemplateBuilderConfigurer(
ObjectProvider<HttpMessageConverters> messageConverters,
ObjectProvider<RestTemplateCustomizer> restTemplateCustomizers,
ObjectProvider<RestTemplateRequestCustomizer<?>> restTemplateRequestCustomizers) {
RestTemplateBuilderConfigurer configurer = new RestTemplateBuilderConfigurer();
configurer.setHttpMessageConverters(messageConverters.getIfUnique());
configurer.setRestTemplateCustomizers(restTemplateCustomizers.orderedStream().collect(Collectors.toList()));
configurer.setRestTemplateRequestCustomizers(
restTemplateRequestCustomizers.orderedStream().collect(Collectors.toList()));
return configurer;
}小結
springboot提供了很多Customizer接口方便用戶自行擴展,非常值得設計組件的時候使用
以上就是springboot的Customizer源碼解析的詳細內容,更多關于springboot Customizer的資料請關注腳本之家其它相關文章!
相關文章
mybatis-flex與springBoot整合的實現(xiàn)示例
Mybatis-flex提供了簡單易用的API,開發(fā)者只需要簡單的配置即可使用,本文主要介紹了mybatis-flex與springBoot整合,具有一定的參考價值,感興趣的可以了解一下2024-01-01
Java調用Oss JDk實現(xiàn)刪除指定目錄下的所有文件
這篇文章主要為大家詳細介紹了Java如何調用Oss JDk實現(xiàn)刪除指定目錄下的所有文件功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2025-03-03
Maven及Springboot配置JDK版本,編碼,源碼打包等方式
這篇文章主要介紹了Maven及Springboot配置JDK版本,編碼,源碼打包等方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Spring容器的創(chuàng)建過程之如何注冊BeanPostProcessor詳解
關于BeanPostProcessor 各位一定不陌生,今天整理的這篇文章總結了如何注冊BeanPostProcessor,文中有非常詳細的圖文示例,需要的朋友可以參考下2021-06-06

