聊聊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 taskExecutorCustomizers,
            ObjectProvider 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 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 messageConverters,
            ObjectProvider restTemplateCustomizers,
            ObjectProvider> 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)