How @Configuration Embodies the Open/Closed Principle in Spring Boot
Spring Boot’s @Configuration annotation serves as a practical implementation of the Open/Closed Principle by providing declarative, type‑safe bean definitions, enabling closed‑for‑modification business logic while remaining open for extension through modular configuration, conditional beans, and auto‑configuration mechanisms.
Understanding @Configuration
@Configuration is a core Spring annotation that marks a class as a configuration class. Methods annotated with @Bean inside define beans that the IoC container will manage.
@Configuration // declare a configuration class
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
@Bean
public DataSource dataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
ds.setUsername("user");
ds.setPassword("password");
return ds;
}
}Open/Closed Principle (OCP) recap
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
Open for extension: add new code to change behavior.
Closed for modification: existing, tested code should not be changed frequently.
The goal is a stable abstraction layer plus an extensible implementation layer.
How @Configuration helps achieve OCP
3.1 Dependency injection – closed for modification
Business code depends on interfaces, not concrete implementations, so changes to implementations do not affect the service.
public interface PaymentProcessor {
void process(Order order);
}
@Service
public class OrderService {
private final PaymentProcessor paymentProcessor;
public OrderService(PaymentProcessor paymentProcessor) {
this.paymentProcessor = paymentProcessor;
}
public void placeOrder(Order order) {
paymentProcessor.process(order);
}
}OrderService only depends on the PaymentProcessor interface.
Switching payment methods (Alipay, WeChat, credit card) requires no changes to OrderService.
3.2 @Configuration – open for extension
Initial configuration provides a bean for a specific implementation.
@Configuration
public class PaymentConfig {
@Bean
public PaymentProcessor paymentProcessor() {
return new AlipayProcessor();
}
}Adding a new implementation (e.g., WeChatPayProcessor) only requires a new class and updating the configuration.
public class WechatPayProcessor implements PaymentProcessor {
@Override
public void process(Order order) {
// WeChat payment logic
}
} @Configuration
public class PaymentConfig {
@Bean
public PaymentProcessor paymentProcessor() {
return new WechatPayProcessor();
}
}3.3 Conditional configuration
Using @ConditionalOnProperty allows switching implementations via a property without code changes.
@Configuration
public class PaymentConfig {
@Bean
@ConditionalOnProperty(name = "payment.provider", havingValue = "alipay")
public PaymentProcessor alipayProcessor() {
return new AlipayProcessor();
}
@Bean
@ConditionalOnProperty(name = "payment.provider", havingValue = "wechat")
public PaymentProcessor wechatPayProcessor() {
return new WechatPayProcessor();
}
}Set payment.provider=wechat in application.properties to activate WeChat support.
Spring Boot auto‑configuration as an OCP exemplar
Auto‑configuration lets developers use starters (e.g., spring-boot-starter-data-jpa) without modifying Spring Boot source. Custom @Configuration can override default beans, providing low‑risk, centralized changes.
@Configuration
public class MyCustomDataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "app.datasource")
public DataSource myDataSource() {
return new HikariDataSource();
}
}When this bean exists, Spring Boot’s default DataSource auto‑configuration backs off.
Architectural perspective
Stable point: business logic (closed for modification).
Variable point: implementation choices and external resources (open for extension).
This aligns with layered architecture: domain layer stays stable, infrastructure layer is replaceable.
Practical recommendations
Always depend on interfaces in business code.
Move volatile factors into @Configuration classes.
Leverage conditional annotations (@Profile, @ConditionalOnProperty) for flexible extensions.
Conclusion
@Configuration is a tool that materializes the Open/Closed Principle in Spring Boot, enabling stable business logic while allowing flexible, low‑risk extensions through declarative configuration and conditional beans.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ray's Galactic Tech
Practice together, never alone. We cover programming languages, development tools, learning methods, and pitfall notes. We simplify complex topics, guiding you from beginner to advanced. Weekly practical content—let's grow together!
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.
