zoukankan      html  css  js  c++  java
  • Spring boot --- 自动配置

    spring boot 自动配置

    指的是针对很多spring 应用程序常见的应用功能,spring boot 能自动提供相关配置。

    spring boot 自动配置加载

        Spring boot自动加载的原理一句话就可以说明: Spring Boot在进行SpringBootApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入到Spring容器。那么这个“META-INF/spring.factories”在哪呢?我们去原来的样例工程中找到引入的Maven依赖中的“spring-boot-xxx.jar”

    # 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
    
    # Application Context Initializers
    org.springframework.context.ApplicationContextInitializer=
    org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,
    org.springframework.boot.context.ContextIdApplicationContextInitializer,
    org.springframework.boot.context.config.DelegatingApplicationContextInitializer,
    org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer
    
    

    spring.factories文件提供了很多核心的配置类的类名,Spring Boot在启动的时候,就会加载这些类名,使用类加载器进行类的实例的创建。spring.factories文件会在springboot 初始化的时候加载进去,具体的源码这里就不贴出来了,可以参考下面的参考资料。

    spring boot autoconfigure

        Spring Boot默认会开启了自动配置,@SpringBootApplication注解点进去

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class))
    public @interface SpringBootApplication {
    	...
    }
    
    

        @EnableAutoConfiguration这个注解能根据类路径下的 jar 包和配置动态加载配置和注入bean,例如 举个例子,比如我在 lib 下放一个 druid 连接池的 jar 包,然后在 application.yml 文件配置 druid 相关的参数,Spring Boot 就能够自动配置所有我们需要的东西,如果我把 jar 包拿掉或者把参数去掉,那 Spring Boot 就不会自动配置。

        我们在spring-boot-autoconfigure-xxx.jar中的 spring.factories看到

    # Initializers
    org.springframework.context.ApplicationContextInitializer=
    org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,
    org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer
    
    # Application Listeners
    org.springframework.context.ApplicationListener=
    org.springframework.boot.autoconfigure.BackgroundPreinitializer
    ....
    
    
    org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,
    org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,
    org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,
    org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,
    org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,
    org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,
    org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,
    org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,
    org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,
    org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,
    org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,
    org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,
    org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,
    
    

    看到我们熟悉的 redis 等等其他常用的工具吗,autoconfigure根据属性配置文件里的进行自动生成相应的 bean .

    补充

    spring ,spring MVC 和 spring boot 的区别

    • Spring 是一个“引擎”;
    • Spring MVC 是基于Spring的一个 MVC 框架 ;
    • Spring Boot 是基于Spring4的条件注册的一套快速开发整合包。

    spring boot 优势

    • 自动配置
    • 很多库的开箱即用

    参考资料

    • spring boot与spring mvc的区别是什么? - 潜龙勿用的回答 - 知乎 https://www.zhihu.com/question/64671972/answer/223383505
    • https://blog.csdn.net/acmman/article/details/81975407 (源码分析,推荐一看)
    • Spring Boot 最核心的 25 个注解,都是干货! - Java技术栈的文章 - 知乎 https://zhuanlan.zhihu.com/p/57689422
  • 相关阅读:
    ADERA3 省选模拟赛 SPOJ LMCONST
    TYVJ 1730 二逼平衡树 线段树套平衡树
    BZOJ 1059 [ZJOI2007]矩阵游戏 二分图匹配
    BZOJ 1056 [HAOI2008]排名系统 Splay+Hash
    OI教会我的
    BZOJ 1055 [HAOI2008]玩具取名 DP
    BZOJ 1058 [ZJOI2007]报表统计 Splay
    为自己而奋斗
    [总结]高斯消元&XOR方程
    我 的 2013
  • 原文地址:https://www.cnblogs.com/Benjious/p/12373536.html
Copyright © 2011-2022 走看看