springBoot--原理分析

2021-01-19 00:23

阅读:379

标签:arm   col   let   图片   err   data   author   EOS   mlu   

起步依赖分析

分析spring-boot-starter-parent

按住Ctrl点击pom.xml中的spring-boot-starter-parent,跳转到了spring-boot-starter-parent的pom.xml,xml配置如下(只摘抄了部分重点配置):

parent>
  groupId>org.springframework.bootgroupId>
  artifactId>spring-boot-dependenciesartifactId>
  version>2.2.5.RELEASEversion>
  relativePath>../../spring-boot-dependenciesrelativePath>
parent>

按住Ctrl点击pom.xml中的spring-boot-starter-dependencies,跳转到了spring-boot-starter-dependencies的pom.xml,xml配置如下(只摘抄了部分重点配置):

properties>
  activemq.version>5.15.11activemq.version>
  antlr2.version>2.7.7antlr2.version>
  appengine-sdk.version>1.9.78appengine-sdk.version>
  artemis.version>2.10.1artemis.version>
  aspectj.version>1.9.5aspectj.version>
  assertj.version>3.13.2assertj.version>
  atomikos.version>4.0.6atomikos.version>
  awaitility.version>4.0.2awaitility.version>
  bitronix.version>2.1.4bitronix.version>
  build-helper-maven-plugin.version>3.0.0build-helper-maven-plugin.version>
  byte-buddy.version>1.10.8byte-buddy.version>
  ...  ....  ...
  
  dependencyManagement>
  dependencies>
    dependency>
      groupId>org.springframework.bootgroupId>
      artifactId>spring-bootartifactId>
      version>2.2.5.RELEASEversion>
    dependency>
    dependency>
      groupId>org.springframework.bootgroupId>
      artifactId>spring-boot-testartifactId>
      version>2.2.5.RELEASEversion>
    dependency>
    ... ... ...
    dependencies>
dependencyManagement>
build>
  pluginManagement>
    plugins>
      plugin>
        groupId>org.apache.johnzongroupId>
        artifactId>johnzon-maven-pluginartifactId>
        version>${johnzon.version}version>
      plugin>
      plugin>
        groupId>org.jetbrains.kotlingroupId>
        artifactId>kotlin-maven-pluginartifactId>
        version>${kotlin.version}version>
      plugin>
      plugin>
        groupId>org.jooqgroupId>
        artifactId>jooq-codegen-mavenartifactId>
        version>${jooq.version}version>
      plugin>
      plugin>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-maven-pluginartifactId>
        version>2.2.5.RELEASEversion>
      plugin>
            ... ... ...
        plugins>
      pluginManagement>
build>

从上面的spring-boot-starter-dependencies的pom.xml中我们可以发现,一部分坐标的版本、依赖管理、插件管理已经定义好,所以我们的SpringBoot工程继承spring-boot-starter-parent后已经具备版本锁定等配置了。所以起步依赖的作用就是进行依赖的传递。

分析spring-boot-starter-web

按住Ctrl点击pom.xml中的spring-boot-starter-web,跳转到了spring-boot-starter-web的pom.xml,xml配置如下(只摘抄了部分重点配置):

 

parent>
  groupId>org.springframework.bootgroupId>
  artifactId>spring-boot-startersartifactId>
  version>2.2.5.RELEASEversion>
parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
version>2.2.5.RELEASEversion>
name>Spring Boot Web Startername>
 dependencies>
    dependency>
      groupId>org.springframework.bootgroupId>
      artifactId>spring-boot-starterartifactId>
      version>2.2.5.RELEASEversion>
      scope>compilescope>
    dependency>
    dependency>
      groupId>org.springframework.bootgroupId>
      artifactId>spring-boot-starter-jsonartifactId>
      version>2.2.5.RELEASEversion>
      scope>compilescope>
    dependency>
    dependency>
      groupId>org.springframework.bootgroupId>
      artifactId>spring-boot-starter-tomcatartifactId>
      version>2.2.5.RELEASEversion>
      scope>compilescope>
    dependency>
    dependency>
      groupId>org.springframework.bootgroupId>
      artifactId>spring-boot-starter-validationartifactId>
      version>2.2.5.RELEASEversion>
      scope>compilescope>
      exclusions>
        exclusion>
          artifactId>tomcat-embed-elartifactId>
          groupId>org.apache.tomcat.embedgroupId>
        exclusion>
      exclusions>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-webartifactId>
      version>5.2.4.RELEASEversion>
      scope>compilescope>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-webmvcartifactId>
      version>5.2.4.RELEASEversion>
      scope>compilescope>
    dependency>
  dependencies>
project>

自动配置原理解析

按住Ctrl点击查看启动类MySpringBootApplication上的注解@SpringBootApplication

package xyz.ytfs;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootQuick2Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootQuick2Application.class, args);
    }

}
/*
 * Copyright 2012-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.annotation.AliasFor;
import org.springframework.data.repository.Repository;

/**
 * Indicates a {@link Configuration configuration} class that declares one or more
 * {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
 * auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
 * annotation that is equivalent to declaring {@code @Configuration},
 * {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
 *
 * @author Phillip Webb
 * @author Stephane Nicoll
 * @author Andy Wilkinson
 * @since 1.2.0
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
      @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

   /**
    * Exclude specific auto-configuration classes such that they will never be applied.
    * @return the classes to exclude
    */
   @AliasFor(annotation = EnableAutoConfiguration.class)
   Class>[] exclude() default {};

   /**
    * Exclude specific auto-configuration class names such that they will never be
    * applied.
    * @return the class names to exclude
    * @since 1.3.0
    */
   @AliasFor(annotation = EnableAutoConfiguration.class)
   String[] excludeName() default {};
       ... ... ...

}

其中,

@SpringBootConfiguration:等同与@Configuration,既标注该类是Spring的一个配置类

@EnableAutoConfiguration:SpringBoot自动配置功能开启

按住Ctrl点击查看注解@EnableAutoConfiguration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    ... ... ...
}

 其中,@Import(AutoConfigurationImportSelector.class) 导入了AutoConfigurationImportSelector类


评论


亲,登录后才可以留言!