spring-cloud / spring-cloud/spring-cloud-commons

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

Open
#1,487 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting-for-triage
Dominant language
Java
Stars
751
Forks
744
Avg merge
1d 14h
Merged PRs (30d)
9

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the MyConfig and MyListener sample as the reproducer, starting with the interaction between @DependsOn and @RefreshScope during startup. Trace why MyListener initializes before myBean; done means the dependency is initialized before the consumer, with the expected output ordering demonstrated by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.