Spring Boot Version
2.6.0+
How to Reproduce
Implments MeterBinder and inject RedisTemplate
@Service
public class InMemoryUserRegistrationService implements UserRegistrationService, MeterBinder {
@Autowired
private RedisTemplate redisTemplate;
@Override
public void bindTo(MeterRegistry registry) {
}
}
Then I just launch the application, and Circular-References are created
Errors Output
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
webMvcMetricsFilter defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.class]
┌─────┐
| prometheusMeterRegistry defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.class]
↑ ↓
| userRegistrationService (field private org.springframework.data.redis.core.RedisTemplate com.xxx.biz.web.service.InMemoryUserRegistrationService.redisTemplate)
↑ ↓
| redisTemplate defined in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]
↑ ↓
| redisConnectionFactory defined in class path resource [org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]
↑ ↓
| lettuceClientResources defined in class path resource [org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]
↑ ↓
| lettuceMetrics defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/redis/LettuceMetricsAutoConfiguration.class]
└─────┘
Action:
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
I ended up having RedisTemplate injected into my business code using @Lazy
Comment From: mhalbritter
Your issue is titled RestTemplate but your code uses RedisTemplate. I assume you mean RedisTemplate in the title too?
Comment From: Kurok1
I've changed the title
Comment From: mhalbritter
I can't reproduce this. If you'd like us to spend some time investigating, please take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.
Comment From: Kurok1
I have create a complete minimal sample) on GitHub. You can direct run DemoApplication and error will be occurred.
Comment From: mhalbritter
Your example uses Spring Boot 2.6.9, which is no longer supported.
But this problem still persists in 2.7.6. It's working with Spring Boot 3.0.0
Comment From: Kurok1
thanks
Comment From: philwebb
I suspect #30636 is the reason this works in 3.0 but I'm not sure we can backport that fix to 2.x
Comment From: Kurok1
Well, I have noticed that in version 3.0 MeterRegistryPostProcessor implementation of change.
But I think most people will not upgrade to the new version quickly, so we have to use ObjectProvider or @Lazy to solve this kind of problem
Comment From: ClaudenirFreitas
Approach to fix this incident:
# application.properties file
spring.main.allow-circular-references=true
See Spring Boot 2.6 Release Notes - Circular References Prohibited by Default.
Comment From: philwebb
I'm going to mark this one as a duplicate of #30636 but unfortunately we don't think we can safely backport the fix for that. Users on Spring Boot 2.x should use the workaround documented above.