一、条件注解架构全景图
二、核心条件注解分类解析
2.1 类路径条件
@Configuration
@ConditionalOnClass(name = "com.fasterxml.jackson.databind.ObjectMapper")
public class JacksonAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public ObjectMapper objectMapper() {
return new ObjectMapper().registerModule(new JavaTimeModule());
}
}
2.2 环境属性条件
@Configuration
@ConditionalOnProperty(
prefix = "app.feature",
name = "cache.enabled",
havingValue = "true",
matchIfMissing = true
)
public class RedisCacheConfig {
@Bean
@ConditionalOnMissingBean
public CacheManager cacheManager(RedisConnectionFactory factory) {
return RedisCacheManager.create(factory);
}
}
2.3 Bean存在条件
@Configuration
@ConditionalOnSingleCandidate(DataSource.class)
public class JdbcTemplateConfig {
@Bean
@ConditionalOnMissingBean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
三、组合条件高级用法
3.1 条件逻辑运算
@Configuration
@Conditional({OnCloudPlatformCondition.class, OnKubernetesCondition.class})
@ConditionalOnExpression(
"${app.cluster.enabled:true} && " +
"${app.metrics.export.enabled:false}"
)
public class ClusterMonitoringConfig {
// 集群监控相关Bean配置
}
3.2 自定义条件注解
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Conditional(OnBusinessDateCondition.class)
public @interface ConditionalOnBusinessDay {
DayOfWeek[] value() default {
DayOfWeek.MONDAY,
DayOfWeek.TUESDAY,
DayOfWeek.WEDNESDAY,
DayOfWeek.THURSDAY,
DayOfWeek.FRIDAY
};
}
public class OnBusinessDateCondition implements Condition {
@Override
public boolean matches(ConditionContext context,
AnnotatedTypeMetadata metadata) {
DayOfWeek today = LocalDate.now().getDayOfWeek();
// 解析注解参数进行判断
return Arrays.asList(metadata.getAnnotationAttributes(
ConditionalOnBusinessDay.class.getName())
.get("value")).contains(today);
}
}
四、条件注解执行原理
4.1 条件判断流程图
4.2 条件评估报告查看
# 启动时添加调试参数
java -jar app.jar --debug
# 控制台输出示例
=========================
CONDITIONS EVALUATION REPORT
=========================
Positive matches:
-----------------
RedisCacheConfig matched:
- @ConditionalOnProperty (app.feature.cache.enabled=true) matched
Negative matches:
-----------------
DataSourceConfig:
- @ConditionalOnClass did not find required class 'com.zaxxer.hikari.HikariDataSource'
五、企业级应用场景
5.1 多环境配置切换
@Profile("dev")
@Configuration
@ConditionalOnProperty(value = "app.env", havingValue = "local")
public class DevDataSourceConfig {
// 开发环境数据源
}
@Profile("prod")
@Configuration
@ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES)
public class ProdDataSourceConfig {
// 生产环境数据源
}
5.2 模块化功能开关
@Configuration
@ConditionalOnFeatureFlag("NEW_PAYMENT_GATEWAY")
public class NewPaymentConfig {
@Bean
public PaymentService paymentService() {
return new StripePaymentService();
}
}
@Configuration
@ConditionalOnMissingFeatureFlag("NEW_PAYMENT_GATEWAY")
public class LegacyPaymentConfig {
@Bean
public PaymentService paymentService() {
return classicPaymentService();
}
}
六、性能优化实践
6.1 条件注解执行耗时
public class TimingCondition implements Condition {
@Override
public boolean matches(ConditionContext context,
AnnotatedTypeMetadata metadata) {
long start = System.nanoTime();
boolean result = // 实际判断逻辑
long duration = System.nanoTime() - start;
log.debug("Condition check took {} ns", duration);
return result;
}
}
6.2 条件缓存优化
@Configuration
@ConditionalOnClass(value = WebClient.class,
name = "reactor.netty.http.client.HttpClient")
@ConditionalOnWebApplication(type = Type.SERVLET)
public class WebClientAutoConfig {
// 组合条件避免重复检查
}
七、常见问题解决方案
7.1 条件注解失效排查
// 检查点清单:
1. 确保配置类被正确扫描
2. 检查条件表达式语法(特别是SpEL表达式)
3. 查看ConditionEvaluationReport
4. 确认属性源加载顺序
5. 检查是否存在多个匹配的Condition实现
7.2 条件执行顺序控制
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnClass(DataSource.class)
public class EarlyInitializationConfig {
// 高优先级配置
}
八、现代扩展应用
8.1 Spring Cloud条件增强
@Configuration
@ConditionalOnConsulEnabled
@ConditionalOnDiscoveryEnabled
public class ConsulServiceDiscoveryConfig {
// Consul服务发现配置
}
8.2 测试环境特殊处理
@Configuration
@ConditionalOnTest
public class MockServiceConfig {
@Bean
@Primary
public PaymentService mockPaymentService() {
return new MockPaymentService();
}
}
诊断工具包包含:
- 条件注解调试检查表
- 条件匹配性能分析工具
- 自定义条件生成器
在线实验环境已就绪:点击访问条件注解沙箱
欢迎提交您遇到的复杂条件匹配场景,我们将提供定制化解决方案!