Using spring.main.lazy-initialization=true
causes @Cacheable
to not work.
Test sample repo: https://github.com/cdalexndr/spring-boot-issue-26470
Spring Boot 2.4.4
Comment From: wilkinsona
Thanks for the sample. You can work around the problem by excluding the CacheInterceptor
(a CacheAspectSupport
) from lazy initialization:
@Bean
LazyInitializationExcludeFilter eagerCacheAspectSupportInitialization() {
return LazyInitializationExcludeFilter.forBeanTypes(CacheAspectSupport.class);
}
It's a SmartInitializingSingleton
and the afterSingletonsInstantiated()
callback isn't invoked for singleton beans that are lazily initialized. This leaves it in an uninitialised state and stops caching from working.
Comment From: wilkinsona
We want to investigate whether we should exclude all SmartInitializingSingleton
s from lazy initialization or tackle them on a case-by-case basis. A concern with doing it for all is that it may result in a cascade of eager initialization that removes much of the benefit of lazy initialization.
Comment From: wilkinsona
Our main concern was Hibernate via HibernateMetricsAutoConfiguration
(a SmartInitializingSingleton
), but it is already eagerly initialized due to LocalContainerEntityManagerFactoryBean
being LoadTimeWeaverAware
.
Comment From: HustonPeng
I ran it under tomcat, it returns 0 always.
So i guess maybe the @AutoConfigureCache
has a problem for setup test.