【每日阅读】2020年7月15日-了解Spring Boot AutoConfiguration

链接

https://www.jianshu.com/p/7e0f67bf3213

文章截图

简评

这篇文章写的非常浅显易懂,个人觉得写的很棒。

这篇文章主要是告诉我们Spring Boot AutoConfiguration是如何实现的。根源就是spring的@Condititon机制。Condition机制可以让一个bean在一定的条件被满足后才注册在spring中。

/**
 * A single {@code condition} that must be {@linkplain #matches matched} in order
 * for a component to be registered.【一个要被bean注册必须满足matches方法】
 *
 * <p>Conditions are checked immediately before the bean-definition is due to be
 * registered and are free to veto registration based on any criteria that can
 * be determined at that point.
 *
 * <p>Conditions must follow the same restrictions as {@link BeanFactoryPostProcessor}
 * and take care to never interact with bean instances. For more fine-grained control
 * of conditions that interact with {@code @Configuration} beans consider the
 * {@link ConfigurationCondition} interface.
 *
 * @author Phillip Webb
 * @since 4.0
 * @see ConfigurationCondition
 * @see Conditional
 * @see ConditionContext
 */
@FunctionalInterface
public interface Condition {

	/**
	 * Determine if the condition matches.
	 * @param context the condition context
	 * @param metadata the metadata of the {@link org.springframework.core.type.AnnotationMetadata class}
	 * or {@link org.springframework.core.type.MethodMetadata method} being checked
	 * @return {@code true} if the condition matches and the component can be registered,
	 * or {@code false} to veto the annotated component's registration
	 */
	boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);

}

例如EmbeddedWebServerFactoryCustomizerAutoConfiguration,这个是spring web项目内嵌server的自动配置类。

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication
@EnableConfigurationProperties(ServerProperties.class)
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {

从前面的分析可以看出是@ConditionalOnWebApplication决定了是否引入这个自动配置类。

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnWebApplicationCondition.class)
public @interface ConditionalOnWebApplication {

从@Conditional(OnWebApplicationCondition.class)可以看到,写匹配逻辑的类是OnWebApplicationCondition类。

class OnWebApplicationCondition extends FilteringSpringBootCondition {
...
@Override
	public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
		boolean required = metadata.isAnnotated(ConditionalOnWebApplication.class.getName());
		ConditionOutcome outcome = isWebApplication(context, metadata, required);
		if (required && !outcome.isMatch()) {
			return ConditionOutcome.noMatch(outcome.getConditionMessage());
		}
		if (!required && outcome.isMatch()) {
			return ConditionOutcome.noMatch(outcome.getConditionMessage());
		}
		return ConditionOutcome.match(outcome.getConditionMessage());
	}
}

这个类内部主要有这么个获取匹配结果的方法,其中主要方法是isWebApplication方法,我就不再赘述了,总之,就是利用Condititon的机制使得spring可以灵活的根据一些条件选择是否注册bean。

原创文章,作者:geekgao,如若转载,请注明出处:https://www.geekgao.cn/archives/2077

(0)
geekgaogeekgao博主
上一篇 2020年7月15日
下一篇 2020年7月16日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

GitHub
分享本页
返回顶部

Warning: error_log(/usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/#log/log-1312.txt): failed to open stream: No such file or directory in /usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/spider.class.php on line 2900