How @Fallback Redefines Spring Bean Selection in 6.2
This article explains the new @Fallback annotation introduced in Spring 6.2, compares it with @Primary, shows code examples of bean configuration, and details the bean selection order changes before and after the 6.2 release.
1. Introduction
When multiple beans of the same type exist in the Spring container, the @Primary annotation is used to indicate which bean should be injected by default.
<code>public interface CommonDAO {}
public class UserDAO implements CommonDAO {}
public class PersonDAO implements CommonDAO {}
public class CommonService {
private CommonDAO dao;
public CommonService(CommonDAO dao) { this.dao = dao; }
}
@Configuration
public class Module1Config {
@Bean
public UserDAO userDAO() { return new UserDAO(); }
@Bean
public CommonService commonService(CommonDAO dao) { return new CommonService(dao); }
}
@Configuration
public class Module2Config {
@Bean
@Primary
public PersonDAO personDAO() { return new PersonDAO(); }
}
</code>Other ways besides @Primary exist; see related articles for alternatives.
2. New Feature: @Fallback
Spring 6.2 introduces the @Fallback annotation, which marks a bean as a fallback candidate and serves as an alternative to @Primary. If several candidates are marked as fallback, the remaining beans are selected. The fallback works only when a single injection point has multiple candidates; for collections, all matching beans are included.
<code>@Configuration
public class AppConfig {
@Bean
public UserDAO userDAO() { return new UserDAO(); }
@Bean
@Fallback
public PersonDAO personDAO() { return new PersonDAO(); }
@Bean
public CommonService commonService(CommonDAO dao) { return new CommonService(dao); }
}
</code>In this example, CommonService receives UserDAO ; if UserDAO is absent, the PersonDAO marked with @Fallback will be injected.
If multiple beans are marked @Fallback without a default, injection for a single point will fail.
<code>@Bean
@Fallback
public UserDAO userDAO() { return new UserDAO(); }
@Bean
@Fallback
public PersonDAO personDAO() { return new PersonDAO(); }
</code>When one bean is @Fallback and another is @Primary, @Primary takes precedence.
<code>@Fallback
public UserDAO userDAO() { }
@Primary
public PersonDAO personDAO() { }
</code>Bean Selection Order in Spring 6.2
@Primary (searched first, then non‑@Fallback)
Match by field or parameter name
@Qualifier (specified value name)
@Priority
Directly registered dependencies (static registration)
Bean Selection Order before Spring 6.2
@Primary
@Priority
Directly registered dependencies
Direct registration can be done manually, for example:
<code>// manual BeanFactory creation
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerResolvableDependency(CommonDAO.class, TeacherDAO.class);
</code>That concludes the article.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.