Closed ChunGeun-Yu closed 1 week ago
Thanks for the sample.
Proxy-based caching uses an advisor to add advice to beans with cacheable methods. Such advisors are created very early which can lead to problems when injecting dependencies into them or into anything upon which they depend. This is done by Spring Framework and, as such, is out of Spring Boot's control.
In your sample, the advisor depends upon your CacheConfigurer
into which you're injecting a RedisConnectionFactory
bean. This leads to early initialization of all of the Redis-related infrastructure.
Note that the problem will occur without a dependency on spring-boot-starter-actuator
and isn't specifically related to MeterRegistryPostProcessor
. I believe it will occur when there is any bean post-processor in the context. For example, without Actuator, the problem still occurs when creating Spring Data's ProjectingArgumentResolverBeanPostProcessor
.
You can eliminate the warnings by reworking your CacheConfiguration
:
@Configuration
@EnableCaching
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
class CacheConfiguration implements CachingConfigurer {
private final ObjectProvider<RedisConnectionFactory> redisConnectionFactory;
CacheConfiguration(ObjectProvider<RedisConnectionFactory> redisConnectionFactory) {
this.redisConnectionFactory = redisConnectionFactory;
}
@Bean
@Override
public CacheManager cacheManager() {
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory.getObject())
.build();
}
}
Using an ObjectProvider
when injecting the connection factory defers the creation of the connection factory. Rather than being created when CacheConfiguration
is created, the creation's now deferred until the cacheManager()
method is called. This addresses all of the warnings for the Redis-related beans. This leaves a single warning about CacheConfiguration
itself. This warning can be avoided by assigning the infrastructure role to CacheConfiguration
which prevents it from being post-processed.
We'll transfer this to the Framework team for their consideration. For example, it may be useful to provide some guidance in the javadoc of CachingConfigurer
.
@wilkinsona Thank you for your detailed explanation.
there are following warn messages
following case. CachingConfigurer + redis cache + jcache(ehcache) + actuator
I've built a minimal sample application to reproduce issue: https://github.com/ChunGeun-Yu/cache-issue-report/tree/master
I think this issue is very similar with https://github.com/spring-projects/spring-boot/issues/29053
full message