Skip to content

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

Open
@huchenhai

Description

@huchenhai

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions