关于springboot:聊聊springboot的Customizer

52次阅读

共计 4240 个字符,预计需要花费 11 分钟才能阅读完成。

本文次要钻研一下 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 接口不便用户自行扩大,十分值得设计组件的时候应用

正文完
 0