smalllove 2019-12-06
打开上面任意一个AutoConfiguration文件,一般都有下面的条件注解,在spring-boot-autoconfigure-1.5.3.RELEASE.jar的org.springframework.boot.autoconfigure.condition包下条件注解如下:
这些注解都组合了@Conditional元注解,只是使用了不同的条件(Conditional),Spring 条件注解(@Conditional)我们介绍过根据不同条件创建不同Bean。
简单分析一下@ConditionalOnWebApplication注解:
package org.springframework.boot.autoconfigure.condition; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.context.annotation.Conditional; @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional(OnWebApplicationCondition.class) public @interface ConditionalOnWebApplication { }
从源码我们可以看出,此注解使用的条件是OnWebApplicationCondition类,下面我们看看这个类是怎么构造的:
package org.springframework.boot.autoconfigure.condition; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.StandardServletEnvironment; @Order(Ordered.HIGHEST_PRECEDENCE + 20) class OnWebApplicationCondition extends SpringBootCondition { private static final String WEB_CONTEXT_CLASS = "org.springframework.web.context." + "support.GenericWebApplicationContext"; @Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { boolean webApplicationRequired = metadata .isAnnotated(ConditionalOnWebApplication.class.getName()); ConditionOutcome webApplication = isWebApplication(context, metadata); if (webApplicationRequired && !webApplication.isMatch()) { return ConditionOutcome.noMatch(webApplication.getMessage()); } if (!webApplicationRequired && webApplication.isMatch()) { return ConditionOutcome.noMatch(webApplication.getMessage()); } return ConditionOutcome.match(webApplication.getMessage()); } private ConditionOutcome isWebApplication(ConditionContext context, AnnotatedTypeMetadata metadata) { if (!ClassUtils.isPresent(WEB_CONTEXT_CLASS, context.getClassLoader())) { return ConditionOutcome.noMatch("web application classes not found"); } if (context.getBeanFactory() != null) { String[] scopes = context.getBeanFactory().getRegisteredScopeNames(); if (ObjectUtils.containsElement(scopes, "session")) { return ConditionOutcome.match("found web application ‘session‘ scope"); } } if (context.getEnvironment() instanceof StandardServletEnvironment) { return ConditionOutcome .match("found web application StandardServletEnvironment"); } if (context.getResourceLoader() instanceof WebApplicationContext) { return ConditionOutcome.match(