您的位置:首页 > 编程语言 > Java开发

springboot自动配置是如何实现的?

2020-04-26 18:58 1176 查看

什么是SpringBoot自动配置?

springboot的自动配置,指的是springboot会自动将一些配置类的bean注册进IOC容器,我们需要的地方使用@Autowired或者@Resource等注解来使用它。
"自动"的表现形式就是我们只需要引我们像用的功能包,相关的配置我们完全不用管,springboot会自动注入这些配置bean,我们直接使用这些bean即可。

SpringBoot如何实现自动配置?

SpringBoot的自动配置,实际是依赖**@Conditional**来实现的。@Conditional是一个条件注解,是Spring4提供的一个新特性,用于根据特定条件来控制Bean的创建行为。
看下@Conditional的实现:

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

/**
* All {@link Condition Conditions} that must {@linkplain Condition#matches match}
* in order for the component to be registered.
*/
Class<? extends Condition>[] value();

}

Conditional 注解类里只有一个 value 属性,是一个Condition 类型的数组。
看下Condition :

@FunctionalInterface
public interface Condition {
/**
* Determine if the condition matches.
* @param context the condition context
* @param metadata 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);
}

从注释中可以得知如果这个方法返回true则会将标注的类注册到容器中。

方法里面有两个参数,AnnotatedTypeMetadata注解元数据类,可以判断一个类是否为注解,或获取一个注解类的所有属性以及对应的值。
ConditionContext则是专门为Condition服务的一个接口,可以从中获取到Spring容器的一些对象信息。

当一个 Bean 被 Conditional 注解修饰时,Spring容器会对数组中所有 Condition 接口的 matches() 方法进行判断,当其中所有 Condition 接口的matches()方法都为 ture 时,才会创建 Bean 。

SpringBoot中对@Conditional的引用链如下

1、main方法所在类的@SpringBootApplication注解
2、点进去之后可以看到SpringBootApplication注解类上有一个@EnableAutoConfiguration

3、EnableAutoConfiguration注解类上有一个@Import(AutoConfigurationImportSelector.class)
在AutoConfigurationImportSelector类中会执行getCandidateConfigurations(AnnotationMetadata metadata,
AnnotationAttributes attributes)方法,里面有一行:
List configurations = SpringFactoriesLoader.loadFactoryNames(
getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
这里就会扫描具有MEAT-INF/spring.factories文件的jar包,得到所有的配置类:
找个spring.factories文件的内容看下:

看下@Conditional是如何作用在这些配置类上的,打开其中一个配置类HttpEncodingAutoConfiguration:
解释上面几个注解:
1、@Configuration标注这个类是一个配置类。
2、@EnableConfigurationProperties//启动指定类的ConfigurationProperties功能;将配置文件中对应的值和HttpEncodingProperties绑定起来;并把HttpEncodingProperties加入到ioc容器中 。
所有在配置文件中能配置的属性都是在xxxxProperties类中封装着;该类中有什么属性,配置文件就可以配置什么;
3、@ConditionalOnWebApplication //Spring底层@Conditional注解:根据不同的条件,如果满足指定的条件,整个配置类里面的配置就会生效; 判断当前应用是否是web应用,如果是,当前配置类生效。
4、@ConditionalOnClass(CharacterEncodingFilter.class) //判断当前项目有没有CharacterEncodingFilter这个类;CharacterEncodingFilter类是SpringMVC中进行乱码解决的过滤器;
5、@ConditionalOnProperty(prefix = “spring.http.encoding”, value = “enabled”, matchIfMissing=true) //判断配置文件中是否存在 spring.http.encoding.enabled这个配置;如果不存在,判断也是成立的。

原文链接:https://blog.csdn.net/insomsia/article/details/88865144?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

  • 点赞
  • 收藏
  • 分享
  • 文章举报
泡沫之夏Z 发布了6 篇原创文章 · 获赞 0 · 访问量 314 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: