Skip to content

Using RedisTemplate in a MeterBinder causes a circular dependency #33488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Kurok1 opened this issue Dec 8, 2022 · 10 comments
Closed

Using RedisTemplate in a MeterBinder causes a circular dependency #33488

Kurok1 opened this issue Dec 8, 2022 · 10 comments
Labels
status: duplicate A duplicate of another issue

Comments

@Kurok1
Copy link

Kurok1 commented Dec 8, 2022

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 8, 2022
@mhalbritter
Copy link
Contributor

Your issue is titled RestTemplate but your code uses RedisTemplate. I assume you mean RedisTemplate in the title too?

@mhalbritter mhalbritter added the status: waiting-for-feedback We need additional information before we can continue label Dec 8, 2022
@Kurok1 Kurok1 changed the title Circular-References cause by using RestTemplate and MeterBinder Circular-References cause by using RedisTemplate and MeterBinder Dec 8, 2022
@Kurok1
Copy link
Author

Kurok1 commented Dec 8, 2022

I've changed the title

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 8, 2022
@mhalbritter
Copy link
Contributor

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.

@mhalbritter mhalbritter added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Dec 8, 2022
@Kurok1
Copy link
Author

Kurok1 commented Dec 8, 2022

I have create a complete minimal sample) on GitHub. You can direct run DemoApplication and error will be occurred.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 8, 2022
@mhalbritter
Copy link
Contributor

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

@mhalbritter mhalbritter added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Dec 8, 2022
@mhalbritter mhalbritter added this to the 2.7.x milestone Dec 8, 2022
@mhalbritter mhalbritter changed the title Circular-References cause by using RedisTemplate and MeterBinder Using RedisTemplate in a MeterBinder causes a circular dependency Dec 8, 2022
@Kurok1
Copy link
Author

Kurok1 commented Dec 8, 2022

thanks

@philwebb
Copy link
Member

I suspect #30636 is the reason this works in 3.0 but I'm not sure we can backport that fix to 2.x

@philwebb philwebb added the for: team-meeting An issue we'd like to discuss as a team to make progress label Dec 10, 2022
@Kurok1
Copy link
Author

Kurok1 commented Dec 11, 2022

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

@ClaudenirFreitas
Copy link

ClaudenirFreitas commented Dec 23, 2022

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.

@philwebb
Copy link
Member

philwebb commented Jan 9, 2023

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.

@philwebb philwebb closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2023
@philwebb philwebb added status: duplicate A duplicate of another issue and removed type: bug A general bug for: team-meeting An issue we'd like to discuss as a team to make progress labels Jan 9, 2023
@philwebb philwebb removed this from the 2.7.x milestone Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

5 participants