Sobes.tech
Senior

There are two beans. How to select one of them by default?

sobes.tech AI

Answer from AI

To select one of two beans by default, configuration mechanisms or annotations are usually used, depending on the framework (for example, Spring in Java).

In Spring, you can use the @Primary annotation to designate the bean that will be used by default during autowiring:

@Bean
@Primary
public MyService myServiceDefault() {
    return new MyServiceImpl1();
}

@Bean
public MyService myServiceAlternative() {
    return new MyServiceImpl2();
}

If @Primary is not suitable, you can use @Qualifier to explicitly specify which bean to inject.

In other systems, similarly, you need to specify priority or select a bean through configuration so that the system knows which one to use by default.