@Configuration
public class A {
@Autowired
private Integer connectTimeOut;
@Bean
public Integer connectTimeOut(){
return getTimeOutConfigured(HTTP_CONNECT_TIME_OUT, -1);
}
}
Springboot can't start successfully with below issue: The dependencies of some of the beans in the application context form a cycle:
┌──->──┐
| A (field private java.lang.Integer XXX.XXX.XXX.XXX.XXXX.XXX.A.connectTimeOut)
└──<-──┘
It can work fine from springboot 2.5.6. But it can't start for 2.6.6.
Any one who has the experience? or any advice?
CodePudding user response:
It sounds like you're using this connectTimeOut
value somewhere, probably in some other bean:
public class MyBean {
private final Integer connectTimeOut;
public MyBean(Integer connectTimeOut) {
this.connectTimeOut = connectTimeOut;
}
}
And your configuration class looks something like this:
@Configuration
public class A {
@Autowired
private Integer connectTimeOut;
@Bean
public Integer connectTimeOut(){
return getTimeOutConfigured(HTTP_CONNECT_TIME_OUT, -1);
}
@Bean
public MyBean myBean() {
return new MyBean(connectTimeOut);
}
}
If so, consider using the built-in mechanism of spring boot to work with configuration properties:
@ConfigurationProperties
public class MyConfigProperties{
private Integer connectTimeOut = -1; // default
// setter, getter, no-args-constructor
}
In application.yml
or properties file define:
application.yml
connectTimeOut: 30
Now you can rewrite the configuration and get rid of the autowiring altogether:
@Configuration
@EnableConfigurationProperties(MyConfigProperties.class)
public class A {
@Bean
public MyBean myBean(MyConfigProperties config) { // Injecting the configuration!!!
return new MyBean(config.getConnectTimeOut());
}
}
CodePudding user response:
A cycle would explain why it worked in 2.5 but not in 2.6. The Spring Boot 2.6 Release Notes describe what's going on and how to resolve the issue:
Circular references between beans are now prohibited by default. If your application fails to start due to a BeanCurrentlyInCreationException you are strongly encouraged to update your configuration to break the dependency cycle. If you are unable to do so, circular references can be allowed again by setting spring.main.allow-circular-references to true, or using the new setter methods on SpringApplication and SpringApplicationBuilder This will restore 2.5’s behaviour and automatically attempt to break the dependency cycle.