@DependsOn does not work when target bean is annotated with @RefreshScope
Summary When a Spring-managed bean uses @DependsOn to indicate it depends on another bean annotated with @RefreshScope, the dependency is not honored during startup. The consumer component doesn't wait the dependent bean initialize first.
Expected Behavior Beans annotated with @DependsOn("beanName") should only initialize after the target beanName is fully initialized. Should see the sysout "dependency myBean initialized", then get sysout "dependency myBean initialized"
Actual Behavior With @RefreshScope being added, The consumer component Mylistener gets generated before MyBean in Myconfig
Sample Configuration To Reproduce Issue
@Configuration
public class MyConfig {
@Bean(name = "myBean")
@RefreshScope
public MyBean myBean() {
System.out.println("dependency myBean initialized");
return new MyBean();
}
}
@Component
@DependsOn("myBean")
@Order(1)
public class MyListener {
@Autowired
private MyBean myBean;
public MyListener() {
System.out.println("MyListener initialized");
}
}```
From my understanding, it's expected behavior, and not specific to @RefreshScope but any @Scope.