spring-cloud-commons icon indicating copy to clipboard operation
spring-cloud-commons copied to clipboard

@DependsOn does not work when target bean is annotated with @RefreshScope

Open huchenhai opened this issue 8 months ago • 1 comments

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");
    }
}```

huchenhai avatar Apr 24 '25 12:04 huchenhai

From my understanding, it's expected behavior, and not specific to @RefreshScope but any @Scope.

quaff avatar Apr 25 '25 08:04 quaff