belowfox
belowfox

Reputation: 71

Multiple @ConditionalOnMissingBean bean which will be used

I have multiple bean be annotated with @ConditionalOnMissingBean, which will be used? How can i control the priority?

Upvotes: 1

Views: 468

Answers (2)

seb
seb

Reputation: 115

The bean whose auto-configuration class is run first will be taken.

There is @AutoConfigureBefore, @AutoConfigureAfter and @AutoConfigureOrder to control the order of auto-configuration classes.

Upvotes: 0

Pilpo
Pilpo

Reputation: 1276

If you want to control @Bean creation ordering, you can use the annotation @Order

@Component
@Order(1)
public class First {

    public int first() {
        return 1;
    }
}

@Component
@Order(2)
public class Second {

    public int second() {
        return 2;
    }
}

Or you can also use @DependsOn

@Configuration
public class ActionCfg {
 
    @Bean
    @DependsOn({"actionA","actionB"})
    public ActionC actionC(){
        return new ActionC();
    }
    
    @Bean("ActionA")
    public ActionA actionA() {
        return new ActionA();
    }
    
    @Bean("ActionB")
    public ActionB actionB() {
        return new ActionB();
    }   
}

ActionA and ActionB will initialized before ActionC.

Upvotes: 1

Related Questions