SpringBoot——启动类的原理

news2024/9/28 11:25:20

优质博文:IT-BLOG-CN

SpringBoot启动类上使用@SpringBootApplication注解,该注解是一个组合注解,包含多个其它注解。和类定义SpringApplication.run要揭开SpringBoot的神秘面纱,我们要从这两位开始就可以了。

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

一、@SpringBootApplication

@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 {

@SpringBootApplication注解上标有三个注解@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan。其它四个注解用来声明SpringBootAppliction为一个注解。

@SpringBootConfiguration

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

@SpringBootConfiguration注解中没有定义任何属性信息,而该注解上有一个注解@Configuration,用于标识配置类。所以@SpringBootConfiguration注解的功能和@Configuration注解的功能相同,用于标识配置类,与@Bean搭配使用,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean。如下案例:

@Configuration 
public class Conf { 
      @Bean 
      public Car car() { 
          return new Car(); 
      }
}

@ComponentScan

@ComponentScan这个注解在Spring中很重要,它对应XML配置中的元素,@ComponentScan的功能其实就是自动扫描并加载符合条件的组件(比如@Component@Repository等)或者bean定义,最终将这些bean定义加载到IoC容器中。我们可以通过basePackages等属性来细粒度的定制@ComponentScan自动扫描的范围,如果不指定,则默认Spring框架实现会从声明@ComponentScan所在类的package进行扫描。注:所以SpringBoot的启动类最好是放在root package下,因为默认不指定basePackages

@EnableAutoConfiguration

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

个人感觉@EnableAutoConfiguration这个Annotation最为重要,Spring框架提供的各种名字为@Enable开头的Annotation,借助@Import的支持,收集和注册特定场景相关的bean定义。@EnableAutoConfiguration也是借助@Import的帮助,将所有符合自动配置条件的bean定义加载到IoC容器。@EnableAutoConfiguration注解上标注了两个注解,@AutoConfigurationPackage@Import@Import注解在SpringIOC一些注解的源码中比较常见,主要用来给容器导入目标bean。这里@Import注解给容器导入的组件用于自动配置:AutoConfigurationImportSelector@AutoConfigurationPackage注解是Spring自定义的注解,用于将主配置类所在的包作为自动配置的包进行管理。

@AutoConfigurationPackage

package org.springframework.boot.autoconfigure;

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

@AutoConfigurationPackage注解上的@Import注解,给容器导入了Registrar组件

Registrar

static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
    Registrar() {
    }

    public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
        AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames().toArray(new String[0]));
    }

    public Set<Object> determineImports(AnnotationMetadata metadata) {
        return Collections.singleton(new AutoConfigurationPackages.PackageImports(metadata));
    }
}

Registrar是抽象类AutoConfigurationPackages的内部静态类,Registrar内的registerBeanDefinitions()方法负责将注解所在的包及其子包下的所有组件注册进容器。这也是为什么SpringBoot的启动类要在其他类的父包或在同一个包中。

AutoConfigurationImportSelector

public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {

借助AutoConfigurationImportSelector@EnableAutoConfiguration可以帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建的IoC容器中。借助于 Spring框架原有的一个工具类:SpringFactoriesLoader@EnableAutoConfiguration的自动配置功能才得以大功告成!

SpringFactoriesLoader属于Spring框架私有的一种扩展方案,其主要功能就是从指定的配置文件META-INF/spring.factories 加载配置。

public abstract class SpringFactoriesLoader {
    public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader classLoader) {
        ......
    }
    public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
        ......
    }
}

配合@EnableAutoConfiguration使用的话,它更多是提供一种配置查找的功能支持,即根据@EnableAutoConfiguration的完整类名org.springframework.boot.autoconfigure.EnableAutoConfiguration作为查找的Key,获取对应的一组@Configuration类。

上图就是从SpringBootautoconfigure依赖包中的META-INF/spring.factories配置文件中摘录的一段内容,可以很好地说明问题。

所以,@EnableAutoConfiguration自动配置的魔法骑士就变成了:从classpath中搜寻所有的META-INF/spring.factories配置文件,并将其中org.springframework.boot.autoconfigure.EnableutoConfiguration对应的配置项通过反射Java Refletion实例化,为标注了@Configuration的配置类加载到IoC容器中。

AutoConfigurationImportSelector类实现了很多Aware接口,而Aware接口的功能是使用一些Spring内置的实例获取一些想要的信息,如容器信息、环境信息、容器中注册的bean信息等。而 AutoConfigurationImportSelector类的作用是将Spring中已经定义好的自动配置类注入容器中,而实现该功能的方法是selectImports方法:

selectImports

注册Spring中定义好的配置类

public String[] selectImports(AnnotationMetadata annotationMetadata) {
    if (!this.isEnabled(annotationMetadata)) {
        return NO_IMPORTS;
    } else {
        AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
        AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata);
        return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
    }
}

@EnableAutoConfigurationSpringBoot根据应用所声明的依赖来对Spring框架进行自动配置。
@SpringBootConfiguration(内部为@Configuration):被标注的类等于在SpringXML配置文件中(applicationContext.xml),装配所有Bean事务,提供了一个Spring的上下文环境。
@ComponentScan:组件扫描,可自动发现和装配Bean,默认扫描SpringApplicationrun方法里的Booter.class所在的包路径下文件,所以最好将该启动类放到根包路径下。

二、SpringApplication.run(x.class, args)

SpringApplicationrun方法的实现是SpringApplication执行流程的主要线路,该方法的主要流程大体可以归纳如下:

【1】如果我们使用的是SpringApplication的静态run方法,那么,这个方法里面首先要创建一个SpringApplication对象实例,然后调用这个创建好的SpringApplication的实例方法。在 SpringApplication实例初始化的时候,它会提前做几件事情:
 ● 根据classpath里面是否存在某个特征类org.springframework.web.context.ConfigurableWebApplicationContext来决定是否应该创建一个为Web应用使用的ApplicationContext类型。
 ● 使用SpringFactoriesLoader在应用的classpath中查找并加载所有可用的ApplicationContextInitializer
 ● 使用SpringFactoriesLoader在应用的classpath中查找并加载所有可用的ApplicationListener
 ● 推断并设置main方法的定义类。

【2】SpringApplication完成实例初始化并且完成设置后,就开始执行run方法的逻辑,首先遍历执行所有通过SpringFactoriesLoader可以查找到并加载的 SpringApplicationRunListener[接口]。调用它们的started()方法,告诉这些SpringApplicationRunListener,“嘿,SpringBoot应用要开始执行咯!”。

public interface SpringApplicationRunListener {
    default void starting() {
    }

    default void environmentPrepared(ConfigurableEnvironment environment) {
    }

    default void contextPrepared(ConfigurableApplicationContext context) {
    }

    default void contextLoaded(ConfigurableApplicationContext context) {
    }

    default void started(ConfigurableApplicationContext context) {
    }

    default void running(ConfigurableApplicationContext context) {
    }

    default void failed(ConfigurableApplicationContext context, Throwable exception) {
    }
}

【3】创建并配置当前Spring Boot应用将要使用的Environment(包括配置要使用的PropertySource以及Profile)。
【4】遍历调用所有SpringApplicationRunListenerenvironmentPrepared()的方法,告诉他们:“当前SpringBoot应用使用的Environment准备好了咯!”。
【5】如果SpringApplicationshowBanner属性被设置为true,则打印banner
【6】根据用户是否明确设置了applicationContextClass类型以及初始化阶段的推断结果,决定该为当前SpringBoot应用创建什么类型的ApplicationContext并创建完成,然后根据条件决定是否添加 ShutdownHook,决定是否使用自定义的BeanNameGenerator,决定是否使用自定义的ResourceLoader,当然,最重要的,将之前准备好的Environment设置给创建好的ApplicationContext使用。
【7】ApplicationContext创建好之后,SpringApplication会再次借助SpringFactoriesLoader,查找并加载classpath中所有可用的ApplicationContextInitializer,然后遍历调用这些 ApplicationContextInitializerinitialize(applicationContext)方法来对已经创建好的ApplicationContext进行进一步的处理。
【8】遍历调用所有SpringApplicationRunListenercontextPrepared()方法。
【9】最核心的一步,将之前通过 @EnableAutoConfiguration获取的所有配置以及其他形式的 IoC容器配置加载到已经准备完毕的ApplicationContext
【10】遍历调用所有SpringApplicationRunListenercontextLoaded()方法。
【11】调用ApplicationContextrefresh()方法,完成IoC容器可用的最后一道工序。
【12】查找当前ApplicationContext中是否注册有CommandLineRunner,如果有,则遍历执行它们。
【13】正常情况下,遍历执行SpringApplicationRunListenerfinished()方法、(如果整个过程出现异常,则依然调用所有SpringApplicationRunListenerfinished()方法,只不过这种情况下会将异常信息一并传入处理)

去除事件通知点后,整个流程如下:

调试一个SpringBoot启动程序为例,参考流程中主要类类图,来分析其启动逻辑和自动化配置原理。

上图为SpringBoot启动结构图,我们发现启动流程主要分为三个部分:
第一部分进行 SpringApplication的初始化模块,配置一些基本的环境变量、资源、构造器、监听器
第二部分实现了应用具体的启动方案,包括启动流程的监听模块、加载配置环境模块、及核心的创建上下文环境模块
第三部分是自动化配置模块,该模块作为SpringBoot自动配置核心,在后面的分析中会详细讨论。在下面的启动程序中我们会串联起结构中的主要功能

SpringBoot启动类

进入run()方法,run()方法创建了一个SpringApplication实例并调用其run()方法。

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
    return (new SpringApplication(primarySources)).run(args);
}

SpringApplication构造器主要为SpringApplication对象赋一些初值。构造函数执行完毕后,回到run()方法

public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
    this.configureHeadlessProperty();
    SpringApplicationRunListeners listeners = this.getRunListeners(args);
    listeners.starting();

    Collection exceptionReporters;
    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
        this.configureIgnoreBeanInfo(environment);
        Banner printedBanner = this.printBanner(environment);
        context = this.createApplicationContext();
        exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
        this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
        this.refreshContext(context);
        this.afterRefresh(context, applicationArguments);
        stopWatch.stop();
        if (this.logStartupInfo) {
            (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
        }

        listeners.started(context);
        this.callRunners(context, applicationArguments);
    } catch (Throwable var10) {
        this.handleRunFailure(context, var10, exceptionReporters, listeners);
        throw new IllegalStateException(var10);
    }

    try {
        listeners.running(context);
        return context;
    } catch (Throwable var9) {
        this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
        throw new IllegalStateException(var9);
    }
}

该方法中实现了如下几个关键步骤:
【1】创建了应用的监听器SpringApplicationRunListeners并开始监听;
【2】加载SpringBoot配置环境ConfigurableEnvironment,如果是通过web容器发布,会加载StandardEnvironment,其最终也是继承了ConfigurableEnvironment,类图如下:

可以看出,*Environment最终都实现了PropertyResolver接口,我们平时通过environment对象获取配置文件中指定Key对应的value方法时,就是调用了propertyResolver接口的getProperty方法;
【3】配置环境Environment加入到监听器对象中SpringApplicationRunListeners
【4】创建run方法的返回对象:ConfigurableApplicationContext(应用配置上下文),我们可以看一下创建方法:

protected ConfigurableApplicationContext createApplicationContext() {
    Class<?> contextClass = this.applicationContextClass;
    if (contextClass == null) {
        try {
            switch(this.webApplicationType) {
            case SERVLET:
                contextClass = Class.forName("org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext");
                break;
            case REACTIVE:
                contextClass = Class.forName("org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext");
                break;
            default:
                contextClass = Class.forName("org.springframework.context.annotation.AnnotationConfigApplicationContext");
            }
        } catch (ClassNotFoundException var3) {
            throw new IllegalStateException("Unable create a default ApplicationContext, please specify an ApplicationContextClass", var3);
        }
    }

    return (ConfigurableApplicationContext)BeanUtils.instantiateClass(contextClass);
}

会先获取显式设置的应用上下文applicationContextClass,如果不存在,再加载默认的环境配置(通过是否是web environment判断),默认选择AnnotationConfigApplicationContext注解上下文(通过扫描所有注解类来加载bean),最后通过BeanUtils实例化上下文对象,并返回。

ConfigurableApplicationContext类图如下:

主要看其继承的两个方向:
LifeCycle 生命周期类,定义了start启动、stop结束、isRunning是否运行中等生命周期空值方法;

ApplicationContext 应用上下文类,其主要继承了beanFactory(bean的工厂类);
【5】回到run方法内,prepareContext方法将listenersenvironmentapplicationArgumentsbanner等重要组件与上下文对象关联;
【6】接下来的refreshContext(context)方法(初始化方法如下)将是实现 spring-boot-starter-*(mybatisredis等)自动化配置的关键,包括spring.factories的加载,bean的实例化等核心工作。SpringIOC源码refresh方法链接 有兴趣的可以看下。

private void refreshContext(ConfigurableApplicationContext context) {
    this.refresh((ApplicationContext)context);
}

//进入 refresh 方法,IOC容器着重分析的方法
public void refresh() throws BeansException, IllegalStateException {
    synchronized(this.startupShutdownMonitor) {
        this.prepareRefresh();
        ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
        this.prepareBeanFactory(beanFactory);

        try {
            this.postProcessBeanFactory(beanFactory);
            this.invokeBeanFactoryPostProcessors(beanFactory);
            this.registerBeanPostProcessors(beanFactory);
            this.initMessageSource();
            this.initApplicationEventMulticaster();
            this.onRefresh();
            this.registerListeners();
            this.finishBeanFactoryInitialization(beanFactory);
            this.finishRefresh();
        } catch (BeansException var9) {
            if (this.logger.isWarnEnabled()) {
                this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
            }

            this.destroyBeans();
            this.cancelRefresh(var9);
            throw var9;
        } finally {
            this.resetCommonCaches();
        }

    }
}

配置结束后,SpringBoot做了一些基本的收尾工作,返回了应用环境上下文。回顾整体流程,SpringBoot的启动,主要创建了配置环境environment、事件监听listeners、应用上下文applicationContext,并基于以上条件,在容器中开始实例化我们需要的Bean,至此,通过SpringBoot启动的程序已经构造完成,接下来我们来探讨自动化配置是如何实现。

自动化配置

之前的启动结构图中,我们注意到无论是应用初始化还是具体的执行过程,都调用了SpringBoot自动配置模块。

在这里插入图片描述

SpringBoot自动配置模块: 该配置模块的主要使用到了SpringFactoriesLoader,即Spring工厂加载器,该对象提供了loadFactoryNames方法,入参为factoryClassclassLoader,即需要传入上图中的工厂类名称和对应的类加载器,方法会根据指定的classLoader,加载该类加器搜索路径下的指定文件,即spring.factories文件,传入的工厂类为接口,而文件中对应的类则是接口的实现类,或最终作为实现类,所以文件中一般为如下图这种一对多的类名集合,获取到这些实现类的类名后,loadFactoryNames方法返回类名集合,方法调用方得到这些集合后,再通过反射获取这些类的类对象、构造方法,最终生成实例。

# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader

# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener

# Error Reporters
org.springframework.boot.SpringBootExceptionReporter=\
org.springframework.boot.diagnostics.FailureAnalyzers

# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\

下图有助于我们形象理解自动配置流程。

在这里插入图片描述

mybatis-spring-boot-starter starter详细内容链接、spring-boot-starter-web等组件的META-INF文件下均含有spring.factories文件,自动配置模块中,SpringFactoriesLoader收集到文件中的类全名并返回一个类全名的数组,返回的类全名通过反射被实例化,就形成了具体的工厂实例,工厂实例来生成组件具体需要的bean。之前我们提到了EnableAutoConfiguration注解,其类图如下:

在这里插入图片描述

可以发现其最终实现了ImportSelector(选择器)和 BeanClassLoaderAware(bean类加载器中间件),重点关注一下 AutoConfigurationImportSelectorselectImports方法。

public String[] selectImports(AnnotationMetadata annotationMetadata) {
    if (!this.isEnabled(annotationMetadata)) {
        return NO_IMPORTS;
    } else {
        AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata);
        return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
    }
}

protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
    if (!this.isEnabled(annotationMetadata)) {
        return EMPTY_ENTRY;
    } else {
        AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
        List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
        configurations = this.removeDuplicates(configurations);
        Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
        this.checkExcludedClasses(configurations, exclusions);
        configurations.removeAll(exclusions);
        configurations = this.getConfigurationClassFilter().filter(configurations);
        this.fireAutoConfigurationImportEvents(configurations, exclusions);
        return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
    }
}

该方法在SpringBoot启动流程Bean实例化前被执行,返回要实例化的类信息列表。如果获取到类信息,Spring自然可以通过类加载器将类加载到jvm中,现在我们已经通过SpringBootstarter依赖方式依赖了我们需要的组件,那么这些组建的类信息在select方法中也是可以被获取到的。

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
    Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
    return configurations;
}

该方法中的getCandidateConfigurations方法,其返回一个自动配置类的类名列表,方法调用了loadFactoryNames方法,查看该方法:

public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
    String factoryTypeName = factoryType.getName();
    return (List)loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
}

在上面的代码可以看到自动配置器会根据传入的factoryType.getName()到项目系统路径下所有的spring.factories文件中找到相应的key,从而加载里面的类。我们就选取这个 mybatis-spring-boot-autoconfigure下的spring.factories文件

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration

进入org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration中,主要看一下类头:

@Configuration
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
@ConditionalOnBean({DataSource.class})
@EnableConfigurationProperties({MybatisProperties.class})
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
public class MybatisAutoConfiguration {

@Configuration,俨然是一个通过注解标注的SpringBean
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class})这个注解的意思是:当存在SqlSessionFactory.class, SqlSessionFactoryBean.class这两个类时才解析 MybatisAutoConfiguration配置类,否则不解析这一个配置类,make sence,我们需要mybatis为我们返回会话对象,就必须有会话工厂相关类;
@CondtionalOnBean(DataSource.class):只有处理已经被声明为beandataSource
@ConditionalOnMissingBean(MapperFactoryBean.class)这个注解的意思是如果容器中不存在name指定的bean则创建bean注入,否则不执行(该类源码较长,篇幅限制不全粘贴);

以上配置可以保证sqlSessionFactorysqlSessionTemplatedataSourcemybatis所需的组件均可被自动配置,@Configuration注解已经提供了Spring的上下文环境,所以以上组件的配置方式与Spring启动时通过mybatis.xml文件进行配置起到一个效果。通过分析我们可以发现,只要一个基于SpringBoot项目的类路径下存在SqlSessionFactory.class, SqlSessionFactoryBean.class,并且容器中已经注册了dataSourceBean,就可以触发自动化配置,意思说我们只要在maven的项目中加入了mybatis所需要的若干依赖,就可以触发自动配置,但引入mybatis原生依赖的话,每集成一个功能都要去修改其自动化配置类,那就得不到开箱即用的效果了。所以SpringBoot为我们提供了统一的 starter可以直接配置好相关的类,触发自动配置所需的依赖(mybatis)如下:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>

这里是截取的mybatis-spring-boot-starter的源码中pom.xml文件中所有依赖:

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
    </dependency>
</dependencies>

因为maven依赖的传递性,我们只要依赖starter就可以依赖到所有需要自动配置的类,实现开箱即用的功能。也体现出SpringBoot简化了Spring框架带来的大量XML配置以及复杂的依赖管理,让开发人员可以更加关注业务逻辑的开发。

三、总结

配置文件定义属性[@Configuration],自动装配到所属的配置类中,然后通过动态代理进入spring容器中。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1242900.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

dxva2+ffmpeg硬件解码(Windows)终结发布

《dxva2超低延迟视频播放器》演示demo下载URL&#xff1a; 【免费】dxva2硬解码超低延迟网络本地播放器资源-CSDN文库 本地播放 截图&#xff1a; rtsp播放截图&#xff08;推送内容为本地桌面&#xff0c;所以是这样的&#xff09; OK&#xff0c;进入主题&#xff1a; 前前…

拼多多商品详情数据接口(Pinduoduo.item_get)

拼多多商品详情数据接口是一种程序化的接口&#xff0c;通过这个接口&#xff0c;商家或开发者可以使用自己的编程技能&#xff0c;对拼多多平台上的商品信息进行查询、获取和更新。这个接口允许商家根据自身的需求&#xff0c;获取商品的详细信息&#xff0c;例如价格、库存、…

交通管理中的利器:揭秘交通监控大屏的优势和应用场景

交通监控大屏是一种用于监控交通情况的大屏展示方式&#xff0c;能够实时展示交通状况、车流量、车速等数据&#xff0c;为交通管理提供数据支持和决策参考。本文将介绍交通监控大屏的优势、应用场景和未来发展趋势。 一、交通监控大屏的优势 交通监控大屏的主要优势包括&…

【double check 读写锁】

使用double check 读写锁 读多写少场景 记录下 //来源 jdbc 中的查询连接信息 //public abstract class ConnectionUrl implements DatabaseUrlContainer public static ConnectionUrl getConnectionUrlInstance(String connString, Properties info) {if (connString null…

基于C#实现Prim算法

图论在数据结构中是非常有趣而复杂的&#xff0c;作为 Web 码农的我&#xff0c;在实际开发中一直没有找到它的使用场景&#xff0c;不像树那样的频繁使用&#xff0c;不过还是准备仔细的把图论全部过一遍。 一、最小生成树 图中有一个好玩的东西叫做生成树&#xff0c;就是用…

电脑出现api-ms-win-crt-runtime-l1-1-0.dll丢失的情况有什么解决办法,dll文件丢失的方法

在使用电脑过程中&#xff0c;有时可能会遇到缺失api-ms-win-crt-runtime-l1-1-0.dll文件的问题&#xff0c;这可能导致某些应用程序无法正常运行。本文将介绍三种解决这个问题的方法&#xff0c;并比较它们的优缺点。 一.解决api-ms-win-crt-runtime-l1-1-0.dll丢失的问题 方…

再添千万级罚单,某银行年内罚款过亿!金融行业合规问题亟待解决

11月17日晚间&#xff0c;国家金融监管总局上海监管局披露行政处罚信息显示&#xff0c;某银行因32项违法违规事实收到两张690万元的大额罚单&#xff0c;合计罚款金额达1380万元。但这并不是银行该今年收到的第一张大额罚单。今年4月28日&#xff0c;该行因在结售汇、外币理财…

C语言——接受一个整形值(无符号),使用函数的递归,按照顺序打印他的每一位。

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>void print(int n) {if(n>9){print(n/10);}printf("%d ",n%10); }int main() {unsigned int num 0;scanf("%d", &num);print(num);return 0; }

【Redis】前言--redis产生的背景以及过程

一.介绍 为什么会出现Redis这个中间件&#xff0c;从原始的磁盘存储到Redis中间又发生了哪些事&#xff0c;下面进入正题 二.发展史 2.1 磁盘存储 最早的时候都是以磁盘进行数据存储&#xff0c;每个磁盘都有一个磁道。每个磁道有很多扇区&#xff0c;一个扇区接近512Byte。…

Colmap三维重建详解与使用方法

图片捕获过程&#xff0c;请遵循以下指导方针以获得最佳重建结果: 1 捕捉具有良好纹理的图像。避免使用完全没有质感的图片(例如&#xff0c;白色的墙或空桌子)。如果场景本身没有足够的纹理&#xff0c;你可以放置额外的背景对象&#xff0c;如海报等。 2 在相似的照明条件下…

Linux常用命令指南以及shell脚本编程

常用快捷键 Tab键补齐命令CtrlA键光标移动到最前CtrlE键光标移动到最后CtrlU键清空当前输入的内容上/下键切换之前输入的命令 文件操作 cd test进入test目录cd…返回上一个目录cd.停留在当前目录 .代表当前目录ls显示当前文件夹下面的所有文件/文件夹等ls -all显示当前文件夹…

柯桥生活日语学习,打工人的日语你会吗?

打工人在日语里有几种说法: アルバイト 这是最常用的称呼,直接对应中文的“打工”。 例句: 学生の頃はスーパーでアルバイトをしていた。(我学生时代在超市打过工。) バイト これはアルバイトの略称でよく使われる。(这是アルバイト的简称,也很常用。) 例句: バイト先が決…

gitea仓库镜像同步至gitlab

1、参考文档&#xff1a;仓库镜像 | Gitea Documentation 2、错误一&#xff1a;账号密码错误问题 解决方法&#xff1a; 出现以上错误为第三步用户名&#xff08;Oauth2应用名称&#xff09;或者密码&#xff08;Gitlab个人访问令牌&#xff09;错误。 1&#xff09;如下图1…

【攻防世界-misc】pure_color

1.方法一&#xff1a;用画图工具打开图片&#xff0c;将图片拷贝至虚拟机win7桌面&#xff0c; 点“属性”&#xff0c;颜色设置为“黑白”&#xff0c; 出现flag值。 2.方法二&#xff1a;使用Stegsilve打开&#xff0c;分析图片 将图片打开&#xff0c;按左右键查找&#xff…

Linux常用基础命令及重要目录,配置文件功能介绍

目录 一&#xff0c;Linux常用必备基础命令 1&#xff0c;网络类命令 2&#xff0c;文件目录类命令 3&#xff0c;操作类命令 4&#xff0c;关机重启命令 5&#xff0c;帮助命令 6&#xff0c;查看显示类命令 7&#xff0c;命令常用快捷键 二&#xff0c;Linux重要目录…

【Linux】:消息队列和信号量

信号 一.消息队列1.原理2.消息队列的各种接口1.创建消息队列2.释放消息队列3.发送和接收信息 二.信号1.概念2.各种接口1.创建信号量2.销毁信号量3.对信号量进行操作 三.一个现象 一.消息队列 1.原理 这种消息队列被称为system V标准。 可以使用ipcs -q来查询消息队列&#xff…

Jmeter+influxdb+grafana监控平台在windows环境的搭建

原理&#xff1a;Jmeter采集的数据存储在infuxdb数据库中&#xff0c;grafana将数据库中的数据在界面上进行展示 一、grafana下载安装 Download Grafana | Grafana Labs 直接选择zip包下载&#xff0c;下载后解压即可&#xff0c;我之前下载过比较老的版本&#xff0c;这里就…

【git】pip install git+https://github.com/xxx/xxx替换成本地下载编译安装解决网络超时问题

目录 &#x1f311;&#x1f311; 背景 &#x1f312; &#x1f312;作用 &#x1f314;&#x1f314; 问题 &#x1f314;&#x1f314;解决方案 &#x1f319;方法一 &#x1f319;方法二 &#x1f31d;&#x1f31d;我的解决方案 整理不易&#xff0c;欢迎一键三连…

Exception in thread “消费者“ java.lang.IllegalMonitorStateException

这两天学习生产者消费者模型的时候&#xff0c;使用Java线程来实现&#xff0c;出现了一个问题“Exception in thread "消费者" java.lang.IllegalMonitorStateException”&#xff0c;并且&#xff0c;线程不结束。报错图片如下&#xff1a; 那我们怎么解决呢&…

品牌小红书koc投放策略分享,纯干货!

作为中国具有影响力的时尚美妆社交平台&#xff0c;小红书与其充满活力的用户群体成为品牌寻找优质KOC合作的理想平台。本文伯乐网络传媒将探讨品牌如何利用小红书的KOC投放策略&#xff0c;实现更广泛的市场覆盖和更有效的品牌营销。 一、明确目标受众与KOC合作需求 在开始策…