Circular Dependency in Spring

Circular dependency in Spring

As the other answers have said, Spring just takes care of it, creating the beans and injecting them as required.

One of the consequences is that bean injection / property setting might occur in a different order to what your XML wiring files would seem to imply. So you need to be careful that your property setters don't do initialization that relies on other setters already having been called. The way to deal with this is to declare beans as implementing the InitializingBean interface. This requires you to implement the afterPropertiesSet() method, and this is where you do the critical initialization. (I would also include code to check that important properties have actually been set.)

circular dependency in web application using spring boot security

you can write one line in your application.properties file to remove this error.

spring.main.allow-circular-references= true

Spring suddenly has cyclic dependencies after switching to new version

As per documentation,
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6.0-M2-Release-Notes#circular-references-prohibited-by-default, Circular references are not prohibited by default. Circular references can be allowed again by setting spring.main.allow-circular-references to true
So, there are chances that your application had circular dependency earlier as well but was working as Spring allowed it by default. Best solution is to identify and remove the circular references.

Spring boot application fails to start after upgrading to 2.6.0 due to circular dependency[ unresolvable circular reference]

Circular References Prohibited by Default in spring boot version 2.6

If your application fails to start due to a BeanCurrentlyInCreationException you are strongly encouraged to update your configuration to break the dependency cycle.

The temporary solution is to restore 2.5’s behavior, set the following in .properties/yml,

spring: 
main:
allow-circular-references: true

See this article for an in-depth description and some possible remedies.

Upgrading to springboot 2.6.7 leads to cyclic dependency

Spring Boot will automatically add every MeterFilter bean to the auto-configured MeterRegistry so your MeterRegistryConfig in its current form isn't needed. I'd replace it with a @Configuration class that defines a MeterFilter bean. Something like this:

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.config.MeterFilter;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import org.jetbrains.annotations.NotNull;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
class MeterRegistryConfig {

@Bean
MeterFilter yourMeterFilter() {
return new MeterFilter() {
@Override
public DistributionStatisticConfig configure(
@NotNull final Meter.Id id,
@NotNull final DistributionStatisticConfig config) {
return DistributionStatisticConfig.builder()
.percentilesHistogram(true)
.percentilePrecision(3)
.build()
.merge(config);
}
};
}

}


Related Topics



Leave a reply



Submit