Spring Boot核心原理-自动配置

来源:互联网 发布:淘宝网售后 编辑:程序博客网 时间:2024/05/16 14:52

作者简介:朱清,毕业于电子科技大学,现任职冰鉴科技高级研发经理,主导冰鉴风控系统架构设计和研发。

之前在公司内部推行spring boot时,有同事跟我提到过,感觉换到spring boot这个框架后,好处是小白也能迅速上手写业务代码了。但是呢,这种情况下新手很容易写得云里雾里的,因为完全不知道背后的原理是什么,相对比在学习spring时需要深刻理解ioc、搞一堆繁琐的配置来说,的确缺少了被迫跳出舒适区去学习一些原理的过程,那么今天就讲讲,为什么spring boot能够如此简单的让我们迅速上手。

Spring boot出现之后,得益于“习惯优于配置”这个理念,再也没有繁琐的配置、难以集成的内容(大多数流行第三方技术都被集成在内)。 那么背后实现的核心原理到底是什么呢? 其实是spring 4.x提供的基于条件配置bean的能力。

Spring boot关于自动配置的源码在spring-boot-autoconfigure-x.x.x.x.jar中,主要包含了如下图所示的配置(并未截全):

图片描述

我们可以在这里看见所有spring boot为我们做的自动配置。通过在application.properties中设置属性:debug=true,可以通过控制台的输出观察自动配置启动的情况:(以下有删减,建议自己运行一下看看)

=========================AUTO-CONFIGURATION REPORTPositive matches:———————— - @ConditionalOnClass classes found: org.springframework.web.servlet.DispatcherServlet (OnClassCondition)  - found web application StandardServletEnvironment (OnWebApplicationCondition) Negative matches:-----------------   ActiveMQAutoConfiguration did not match      - required @ConditionalOnClass classes not found: javax.jms.ConnectionFactory,org.apache.activemq.ActiveMQConnectionFactory (OnClassCondition)

运行原理

在第一次使用spring boot的时候,大家都会惊讶于@SpringBootApplication这个注解,有了它马上就能够让整个应用跑起来。实际上它只是一个组合注解,包含@Configuration、@EnableAutoConfiguration、@ComponentScan这三个注解。

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@Configuration@EnableAutoConfiguration@ComponentScanpublic @interface SpringBootApplication {    Class<?>[] exclude() default {};    String[] excludeName() default {};    @AliasFor(        annotation = ComponentScan.class,        attribute = "basePackages"    )    String[] scanBasePackages() default {};    @AliasFor(        annotation = ComponentScan.class,        attribute = "basePackageClasses"    )    Class<?>[] scanBasePackageClasses() default {};}

它的核心功能是由@EnableAutoConfiguration这个注解提供的,我们来看看@EnableAutoConfiguration的源代码:

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@AutoConfigurationPackage@Import({EnableAutoConfigurationImportSelector.class})public @interface EnableAutoConfiguration {    Class<?>[] exclude() default {};    String[] excludeName() default {};}

这里的关键功能是@Import注解导入的配置功能
EnableAutoConfigurationImportSelector使用SpringFactoriesLoader.loadFactoryNames方法来扫描具有META-INF/spring.factories文件的jar包,spring-boot-autoconfigure-x.x.x.x.jar里就有一个spring.factories文件,这个文件中声明了有哪些要自动配置。

下面我们来分析一下spring boot autoconfigure里面的MongoAutoConfiguration(mongodb的自动配置),相信你就会明白这套自动配置机制到底是怎么一回事儿:

//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//package org.springframework.boot.autoconfigure.mongo;import com.mongodb.MongoClient;import com.mongodb.MongoClientOptions;import java.net.UnknownHostException;import javax.annotation.PreDestroy;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.boot.autoconfigure.mongo.MongoProperties;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;@Configuration@ConditionalOnClass({MongoClient.class})  @EnableConfigurationProperties({MongoProperties.class}) //开启属性注入。@ConditionalOnMissingBean(    type = {"org.springframework.data.mongodb.MongoDbFactory"}) public class MongoAutoConfiguration {    @Autowired    private MongoProperties properties;    @Autowired(        required = false    )    private MongoClientOptions options;    @Autowired    private Environment environment;    private MongoClient mongo;    public MongoAutoConfiguration() {    }    @PreDestroy    public void close() {        if(this.mongo != null) {            this.mongo.close();        }    }    @Bean //使用java配置,当容器中没有这个bean的时候执行初始化    @ConditionalOnMissingBean    public MongoClient mongo() throws UnknownHostException {        this.mongo = this.properties.createMongoClient(this.options, this.environment);        return this.mongo;    }}

首先这被@Configuration注解了,是一个配置类,当满足以下条件这个bean被装配:

  • 当MongoClient在类路径下。

  • 当容器中没有org.springframework.data.mongodb.MongoDbFactory这类bean的时候。

此外,我们可以看一下通过@EnableConfigurationProperties({MongoProperties.class}) 自动注入的属性(这是习惯优于配置的最终落地点):

@ConfigurationProperties(    prefix = "spring.data.mongodb")public class MongoProperties {    public static final int DEFAULT_PORT = 27017;    private String host;    private Integer port = null;    private String uri = "mongodb://localhost/test";    private String database;    private String authenticationDatabase;    private String gridFsDatabase;    private String username;    private char[] password;    private Class<?> fieldNamingStrategy;    ......}

所以在我们什么都不干的情况下,只需要引入spring-data-mongodb这个依赖再加上默认的MongoDB server我们就能够快速集成MongoDB,用MongodbTemplate访问数据库。

同时我们可以通过在application.yaml中修改spring.data.mongodb相关的参数就能够修改连接配置,如:

spring:    data:        mongodb:            host: localhost            port: 27017            username: chingzhu            password: test123            database: icekredit

利用这套原理,我们也可以轻松地把目前spring boot还未集成的、我们自己要使用的第三方技术自动集成起来。

现在,不知道你对spring boot的机制有一个清楚的认识了吗?

附:常见org.springframework.boot.autoconfigure.condition包下的条件注解意思

@ConditionalOnBean:当容器里有指定的bean的条件下。

@ConditionalOnMissingBean:当容器里不存在指定bean的条件下。

@ConditionalOnClass:当类路径下有指定类的条件下。

@ConditionalOnMissingClass:当类路径下不存在指定类的条件下。

@ConditionalOnProperty:指定的属性是否有指定的值,比如@ConditionalOnProperties(prefix=”xxx.xxx”, value=”enable”, matchIfMissing=true),代表当xxx.xxx为enable时条件的布尔值为true,如果没有设置的情况下也为true。

0 0
原创粉丝点击