在《SpringBoot运作原理解析之加载AutoConfiguration》中我们已经介绍了SpringBoot对配置文件的加载及相应类的实例化操作。那么,SpringBoot是如何之后该实例化哪些类的呢?这篇文章带大家了解一下 @Conditional 注解及其发挥的作用。
@Conditional 注解可以根据是否满足某一个特定条件来决定要不要创建某个特定的Bean。比如,当某一个jar包在一个类路径下的时自动配置一个或多个Bean;或者只有某个Bean被创建才会创建另外一个Bean。该注解由Spring4开始提供,源代码如下:
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Conditional { Class<? extends Condition>[] value(); }
SpringBoot也正是使用 @Conditional 的这项功能来实现自动配置的。SpringBoot对该注解进行了相应个扩展,形成了以下组合注解,以满足更多的情况。
以上组合注解均位于spring-boot-autoconfigure jar包下的org.springframework.boot.autoconfigure.condition包下。
了解组合注解,现在以一个简单的注解 @ConditionalOnJava 来说明一下组合注解的简单实用。 @ConditionalOnJava 的源码为:
@Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional(OnJavaCondition.class) public @interface ConditionalOnJava { Range range() default Range.EQUAL_OR_NEWER; JavaVersion value(); enum Range { EQUAL_OR_NEWER, OLDER_THAN } }
很明显,它是由 @Conditional 注解组合而成。在 @Conditional 中需要满足OnJavaCondition.class定义的条件。OnJavaCondition类代码如下:
@Order(Ordered.HIGHEST_PRECEDENCE + 20) class OnJavaCondition extends SpringBootCondition { private static final JavaVersion JVM_VERSION = JavaVersion.getJavaVersion(); @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { Map<String, Object> attributes = metadata .getAnnotationAttributes(ConditionalOnJava.class.getName()); Range range = (Range) attributes.get("range"); JavaVersion version = (JavaVersion) attributes.get("value"); return getMatchOutcome(range, JVM_VERSION, version); } protected ConditionOutcome getMatchOutcome(Range range, JavaVersion runningVersion, JavaVersion version) { boolean match = isWithin(runningVersion, range, version); String expected = String.format( (range != Range.EQUAL_OR_NEWER) ? "(older than %s)" : "(%s or newer)", version); ConditionMessage message = ConditionMessage .forCondition(ConditionalOnJava.class, expected) .foundExactly(runningVersion); return new ConditionOutcome(match, message); } private boolean isWithin(JavaVersion runningVersion, Range range, JavaVersion version) { if (range == Range.EQUAL_OR_NEWER) { return runningVersion.isEqualOrNewerThan(version); } if (range == Range.OLDER_THAN) { return runningVersion.isOlderThan(version); } throw new IllegalStateException("Unknown range " + range); } }
通过源代码可以看出,OnJavaCondition继承了SpringBootCondition类,并实现了它的getMatchOutcome方法。该方法的实现主要做了以下事情:
同样在spring-boot-autoconfigure jar包下的org.springframework.boot.autoconfigure包下,springboot默认提供了一些自动配置类。随便打开一个AutoConfiguration类都会看到使用到上面的注解:
@Configuration @EnableConfigurationProperties(HttpProperties.class) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @ConditionalOnClass(CharacterEncodingFilter.class) @ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true) public class HttpEncodingAutoConfiguration { private final HttpProperties.Encoding properties; public HttpEncodingAutoConfiguration(HttpProperties properties) { this.properties = properties.getEncoding(); } @Bean @ConditionalOnMissingBean public CharacterEncodingFilter characterEncodingFilter() { CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setEncoding(this.properties.getCharset().name()); filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST)); filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE)); return filter; } @Bean public LocaleCharsetMappingsCustomizer localeCharsetMappingsCustomizer() { return new LocaleCharsetMappingsCustomizer(this.properties); } ……
正因为SpringBoot在spring.factories文件中加载的类都拥有 @Conditional 的扩展注解,SpringBoot便可以判断该AutoConfiguration配置类是否满足 @Conditional *所注解的前置条件,如果满足则进行实例化,如果不满足则跳过。
本篇文章我们了解 @Conditional 的基本使用和在SpringBoot中发挥的作用。后面我们将以具体的示例来进行详细说明。欢迎持续关注。