The TaskExecutorMetricsAutoConfiguration introduced in SB 2.6 causes all Executor beans to be initialized even for cases when they aren't needed. I noticed it for beans that are marked with @Lazy and Prototype beans.

Here is a simple project that demonstrates it.

My recommendation is to change this part with the this:

    @Autowired
    public void bindTaskExecutorsToRegistry(ApplicationContext context, MeterRegistry registry) {
        for (String beanName : context.getBeanNamesForType(Executor.class, false, false)) {
            String executorName = getExecutorName(beanName);
            Executor executor = context.getBean(beanName, Executor.class);
                         ...

That way you avoid the early initialization and non-singleton beans.

Comment From: wilkinsona

Thanks for the suggestion but this behaviour is intentional. We want to provide metrics for all beans of a particular type, even if they've been marked as lazy. If we changed things as you have suggested, metrics would be lost for any Executor beans that have not been initialized at the point. This would affect more executor beans than those that are marked as lazy.

If you do not want metrics for all executor beans, you should exclude the auto-configuration.

Comment From: asibross

Thanks for the suggestion of excluding this AutoConfiguration - we are currently doing this in our apps. But I do feel that this behavior is misleading for Lazy and Prototype beans, which their creation may occur after the application was already initialized or may not occur at all.