Spring Bean Naming Conventions
Fast recall
Quick revision
Core answer
Basic understanding of Spring's bean naming conventions and why names matter
Not knowing the default lowercase-first-letter convention or that you can have multiple beans of the same type distinguished by name
Explain when you'd need custom names: when you have multiple beans of the same type and use @Qualifier to select which one to inject
Interview-ready guide
Interview answer
@Service("billingService") assigns an explicit component name. @Bean({"paymentClient", "primaryPaymentClient"}) can give one object a main name and alias. @Qualifier("live") on a bean and injection point selects a semantic candidate even when the bean has another name. A string qualifier can also fall back against a bean name, which is why the two concepts are related but not identical.
For example, @Component on PaymentService normally registers paymentService, while @Component("cardPaymentService") gives it an explicit name that can be referenced by @Qualifier("cardPaymentService").
Spring Bean Naming Conventions
Direct Answer
Spring assigns a default name to a scanned component from its class name using JavaBeans-style decapitalization, so UserService becomes userService while an acronym name such as URLService may remain URLService. An @Bean method uses its method name by default. @Component/@Bean name attributes set names; @Qualifier adds candidate-selection metadata and does not generally rename the bean.
Code Example
// Default: bean name is "orderService"
@Service
public class OrderService { }
// Custom name: bean is now "orderSvc"
@Service("orderSvc")
public class OrderService { }
@Configuration
public class DatabaseConfig {
// Bean name is "primaryDb" (method name)
@Bean
public DataSource primaryDb() {
return new HikariDataSource();
}
// Custom name: bean is "reportsDatabase"
@Bean(name = "reportsDatabase")
public DataSource secondaryDb() {
return new HikariDataSource();
}
}
@Service
public class ReportService {
private final DataSource dataSource;
public ReportService(@Qualifier("reportsDatabase") DataSource ds) {
this.dataSource = ds; // Gets the secondaryDb bean
}
}
Common Mistakes & Pitfalls
- Not knowing the lowercase convention: Freshers often look for a bean named "UserService" when Spring registered it as "userService"—the default name lowercases the first letter.
- Forgetting that @Bean uses method name: If your method is named
customerRepo()but returns a CustomerRepository, the bean name is "customerRepo" not "customerRepository"—you must use that exact name in @Qualifier. - Using custom names everywhere: Don't customize names unless you need to—if you only have one UserService bean, the default name works fine and changing it just adds confusion. Only customize when you have multiple beans of the same type or when the default name isn't clear enough.
Check your understanding
Answer these without looking back at the article.
- How does @Qualifier use bean names?
- Can two beans have the same name?
- How do you look up a bean by name programmatically?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.