springboot 自动装配_springboot 自动装配

本文介绍了Spring Boot自动装配的相关内容。自动装配可根据特定条件将配置类的bean注册进IOC容器,通过@EnableAutoConfiguration+@Conditional实现。还给出使用Redis的例子,展示自动装配优势,介绍了真实自动装配类及自定义start项目实现自动装配功能。

springboot最大特点就是自动装配

一.什么是自动配置?

根据特定条件自动将一些配置类的bean注册进ioc容器,使得可以在需要的地方直接从容器中获取和使用​ “自动”的表现形式就是我们只需要引我们想用功能的jar包,相关的配置我们完全不用管(springboot内置了很多的**start配置信息)

二.如何实现自动配置?

**@EnableAutoConfiguration+@Conditional**

1.spring boot启动run方法的时候,要有一个@SpringBootApplication注解的资源类(属于@Configuration),prepareContext方法时加载进容器

2.SpringBootApplication注解类上有一个@EnableAutoConfiguration

3.EnableAutoConfiguration注解类上有一个@Import(AutoConfigurationImportSelector.class)

4.spring创建上下文,注册ConfigurationClassPostProcessor.class的BeanDefinition类定义(实现BeanDefinitionRegistryPostProcessor接口)到容器中

5.spring容器在刷新refresh的时候会调用invokeBeanFactoryPostProcessors(beanFactory)方法,实例化ConfigurationClassPostProcessor对象

6.有了ConfigurationClassPostProcessor对象,执行BeanDefinitionRegistryPostProcessor##postProcessBeanDefinitionRegistry的方法

7.BeanDefinitionRegistryPostProcessor##postProcessBeanDefinitionRegistry方法中,实例化ConfigurationClassParser对象并执行parser方法,操作容器中所有带注解的类(@SpringBootApplication),并找到@Import注解后面跟的value值,如果value值类型是DeferredImportSelector接口类型,放到集合中,循环执行getCandidateConfigurations方法

8.在AutoConfigurationImportSelector##getCandidateConfigurations(AnnotationMetadata metadata,AnnotationAttributes attributes)方法,里面有一行:List configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());注意loadFactoryNames方法的这两个参数!扫描所有jar包类路径下/META-INF/spring.factories文件中的EnableAutoConfiguration,能得到很多配置类(包括自定义的和org.springframework.boot.autoconfigure项目的所有)

9.在上面得到的很多List configurations的,删除重复的、excluded的、过滤掉OnClassConditional、OnBeanConditional、OnWebApplicationConditional匹配不上的

10.然后触发AutoConfigurationImportEvent事件,告诉ConditionEvaluationReport条件评估报告器对象来分别记录符合条件和exclude的自动配置类,最后spring再将筛选成功后的类导入IOC容器中

11.org.springframework.boot.autoconfigure项目内置了很多的自动配置,所以如果需要使用这些配置,直接引用对应的jar包(或者引用对应的start jar包,效果一样的),其实spring-boot-starter-XXX这个项目也就是引入一下XXX需要的包而已,其他什么都没做,所以自动引用和使用start引用,没有什么区别

三.例子

在springboot之前,我们要在自己的spring应用中使用redis,要引入redis.clients、spring-data-redis两个引用,并在项目中自己设置配置

有了springboot后,只需要引入spring-boot-starter-redis一个引用,不用自己设置配置;或者引入redis.clients、spring-data-redis两个引用,不用自己设置配置

663f78a568dec8c6ebe1fca8154a1b6b.png

使用autoconfigure内置自动装配

四.看一个真实的自动装配org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration(autoconfigure项目中,引入了很多的jar包依赖,但都是编译时依赖,打成的autoconfigure.jar包中,不会有对应的class类存在!)

fed24f21e9f3d0d5df2c24162f549126.png

autoconfigure自动装配

上面几个注解:

1.@Configuration标注这个类是一个配置类

2.@ConditionalOnClass(RedisOperations.class):判断当前classpath下是否存在RedisOperations.class,若存在则将当前的配置类装载入spring容器

3.@ConditionalOnMissingBean(name = "redisTemplate"):判断当前spring容器中是否存在name为redisTemplate的对象,若不存在则将当前的对象装载入spring容器

org.springframework.boot.autoconfigure.data.redis.RedisProperties 提供了一些默认的配置信息

0420b9d49f4d2b8abd188ea7ab213c57.png

properties属性配置

五.自定义start

自定义一个****start项目,写自己的***configuration和****properties,META-INF/spring.factories,key:org.springframework.boot.autoconfigure.EnableAutoConfiguration value值指向自己的***configuration,项目中引入自定义的start包即可

b11c937fffeb07cb1916d63661026e60.png

自定义start自动装配功能

Spring框架是主要的开源应用程序开发框架,它使得Java/J2EE开发更容易、效率更高。本书不仅向读者展示了Spring能做什么?而且揭示了Spring完成这些功能的原理,解释其功能和动机,以帮助读者使用该框架的所有部分来开发成功的应用程序。本书涵盖Spring的所有特性,并且演示了如何将其构成一个连贯的整体,帮助读者理解Spring方法的基本原理、何时使用Sping以及如何效仿最佳实践。所有这些都以完整的样本应用程序来举例说明。读完本书,读者将能够有效地在各种应用中(从简单的web应用程序到复杂的企业级应用程序)使用Spring。 作者:(美)约翰逊 译者:蒋培 Rod Johnsn是Spring框架的创始人,并且是Java和J2EE领域的著名专家。   Rod获悉民大学博士学位。他具有C/C++开发背景,从Java和J2EE发布以来就作为开发者、休系结构设计者和顾问涉足了Java和J2EE领域。   他撰写了两本最普及并最有影响力的J2EE书籍:《Expert One-on-one J2EE Design and Deuelo Pment》(Wrox,2002),和《J2EE without EJB》(Wrox,2004.与Juergen Hoelle合著)。这两本书在“敏捷J2EE的兴起和心变过度复杂的传统J2EE体系结构方面都起了得要作用。   Rod是Spring框架的共同领导之一。他的发言很受欢迎,并且经常出现在美国、欧洲和亚洲举行的Java重要活动中。了是一些JSR的专家组的成员,为Java社区发展计划(Java Community Process,JCP)EBTL。   他还具有在银行和金融、保险、软件、媒体等领域从事顾问的广泛经验。他是Interface21的CEO,Interface21是一家致力于提供专家级J2EE和Spring框架服务的咨询公司。他各极参与客户项目和Spring开发。
Spring 5 Design Patterns is for all Java developers who want to learn Spring for the enterprise application. Therefore, enterprise Java developers will find it particularly useful in the understanding of design patterns used by the Spring Framework and how it solves common design problems in the enterprise application, and they will fully appreciate the examples presented in this book. Before reading this book, readers should have basic knowledge of Core Java, JSP, Servlet, and XML. Spring 5 Framework is newly launched by Pivotal with reactive programming. Spring 5 introduces many new features and enhancements from its previous version. We will discuss all this in the book. Spring 5 Design Patterns will give you in-depth insight about the Spring Framework. The great part of today's Spring Framework is that all companies have already taken it as a primary framework for development of the enterprise application. For Spring, no external enterprise server is needed to start working with it. The goals of writing this book are to discuss all design patterns used behind the Spring Framework and how they are implemented in the Spring Framework. Here, the author has also given you some best practices that must be used in the design and development of the application. The book contains 12 chapters that cover everything from the basics to more complex design pattern such as reactive programming. Spring 5 Design Patterns is divided into three sections. The first section introduces you to the essentials of the design patterns and the Spring Framework. The second section steps behind the front end and shows where Spring fits in the back end of an application. The third section expands on this by showing how to build web applications with Spring and introducing a new feature of the Spring 5 reactive programming. This part also shows how to handle concurrency in the enterprise application.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值