Java-Based Container Configuration in Spring
Interview Preparation Hub for Backend and Cloud-Native Engineering Roles
1. Introduction
Spring applications can be configured in multiple ways: XML, annotations, and Java-based configuration.
Java-based container configuration uses plain Java classes annotated with @Configuration and @Bean to define beans and their dependencies.
This approach is type-safe, IDE-friendly, and eliminates the verbosity of XML.
2. Core Annotations
| Annotation | Description | Example |
|---|---|---|
| @Configuration | Marks a class as a source of bean definitions. | @Configuration |
| @Bean | Defines a bean inside a @Configuration class. | @Bean |
| @Import | Imports other configuration classes. | @Import(DatabaseConfig.class) |
| @PropertySource | Loads properties from external files. | @PropertySource("classpath:application.properties") |
3. Realistic Example: Banking Application
Consider a banking system where services and repositories are configured using Java-based configuration:
@Configuration
public class BankingConfig {
@Bean
public DataSource dataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost:3306/bank");
ds.setUsername("root");
ds.setPassword("password");
return ds;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean
public AccountRepository accountRepository(JdbcTemplate jdbcTemplate) {
return new AccountRepository(jdbcTemplate);
}
@Bean
public AccountService accountService(AccountRepository repo) {
return new AccountService(repo);
}
}
Here, beans are defined in Java classes, and dependencies are injected automatically by the IoC container.
4. Diagram: Java-Based Configuration Flow
@Configuration Class → @Bean Methods → IoC Container → Bean Instantiation → Dependency Injection → Application Ready
5. Advantages
- Type safety and IDE support.
- Cleaner and more concise than XML.
- Supports modular configuration with @Import.
- Easy integration with external property files.
- Works seamlessly with Spring Boot auto-configuration.
6. Best Practices
- Organize configuration classes by domain (DatabaseConfig, ServiceConfig).
- Use @PropertySource for externalized configuration.
- Leverage constructor injection in @Bean methods.
- Combine with annotation-based configuration for flexibility.
- Keep configuration classes lightweight and focused.
7. Common Mistakes
- Mixing XML and Java configs inconsistently.
- Hardcoding values instead of using properties.
- Placing business logic inside configuration classes.
- Not modularizing configuration → monolithic AppConfig.
8. Interview Notes
- Be ready to explain the difference between XML and Java-based configuration.
- Discuss @Configuration and @Bean usage.
- Explain how @Import supports modular configuration.
- Know how to externalize properties with @PropertySource.
- Understand integration with Spring Boot auto-configuration.
9. Summary
Java-based container configuration in Spring provides a modern, type-safe, and maintainable way to define beans and dependencies. It leverages annotations like @Configuration, @Bean, @Import, and @PropertySource to replace XML configuration. For interviews, focus on core annotations, realistic examples, advantages, and best practices. Mastery of Java-based configuration demonstrates readiness for backend engineering and modern Spring Boot development roles.