如何在建设厅网站搜索企业,重庆企业网站推广价格,超值的扬中网站建设,网络营销方法和手段SpringBoot自动化配置原理
01-SpringBoot2高级-starter依赖管理机制
目的#xff1a;通过依赖能了解SpringBoot管理了哪些starter
讲解#xff1a; 通过依赖 spring-boot-dependencies 搜索 starter- 发现非常多的官方starter#xff0c;并且已经帮助我们管理好了版本。 …SpringBoot自动化配置原理
01-SpringBoot2高级-starter依赖管理机制
目的通过依赖能了解SpringBoot管理了哪些starter
讲解 通过依赖 spring-boot-dependencies 搜索 starter- 发现非常多的官方starter并且已经帮助我们管理好了版本。 项目中使用直接引入对应的 starter 即可这个场景下需要的依赖就会自动导入到项目中简化了繁琐的依赖。 如果需要修改版本可以有两种方式 重写maven属性使用Maven依赖管理的就近原则 引入 starter 不仅仅是帮助我们管理了依赖还帮我做了很多的默认的配置信息简化了大量的配置使用更加的简单。 所有的场景启动器的底层都依赖 spring-boot-starter dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactIdversion2.3.10.RELEASE/versionscopecompile/scope
/dependency小结
引入官方starter依赖默认都可以不写版本如果配置满足您当前开发需要则默认配置即可
02-SpringBoot2高级-自动化配置初体验
目的以web MVC自动化配置原理为例讲解能够理解web MVC自动化配置加入了哪些依赖做了哪些默认配置。
讲解
回忆一下SpringMVC学习时候我们在 SSM整合时;
添加spring及spring web mvc相关依赖
springmvc配置类
1、扫描controller层
2、静态资源控制
3、…
servlet容器配置类
1、扫描springmvc配置类
2、扫描spring配置类
3、设置哪些请求交给springmvc处理
4、POST请求乱码过滤器
部署还需要单独的tomcat
也就是说我们现在需要在开发业务代码前就必须要准备好这些环境否则无法完成业务代码这就是我们现在的问题。
让这些问题成为过去现在我们就探索一下SpringBoot是如何帮助我们完成强大而又简单自动化配置的。
引入 web 开发场景启动器依赖
!--web开发的起步依赖 场景启动器依赖--
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency帮助我们做了以下自动化配置
依赖版本和依赖什么jar都不需要开发者关注自动化配置 自动配好SpringMVC 引入SpringMVC全套组件自动配好SpringMVC常用组件三大组件文件上传等 自动配好Web常见功能如字符编码问题静态资源管理 自动配好Tomcat
小结
有了SpringBoot以后让开发人员重点关注业务本身而不是环境上提升了开发效率。
03-SpringBoot2高级-底层原理-Configuration配置注解
目的掌握Configuration注解的作用及新特性
讲解
1、Configuration注解的作用是替代原始 spring配置文件 功能
演示
1编写配置类
package com.itheima.sh.config;import com.itheima.sh.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 1、Configuration 替代 spring配置文件配置bean* 2、组件源码中包含 Component 注解当前类也会注册到 IOC 容器默认类名小写* 3、默认都是单例的*/
Configuration
public class MyConfig {Bean // 默认方法名称作为容器中的namepublic User getUser() {return new User();}
}2在引导类编写代码测试
SpringBootApplication
MapperScan(basePackages com.itheima.sh.mapper)
public class DataApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext SpringApplication.run(DataApplication.class, args);// 根据name获取容器中的beanUser user1 applicationContext.getBean(getUser, User.class);User user2 applicationContext.getBean(getUser, User.class);System.out.println(user1 user2);MyConfig myConfig1 applicationContext.getBean(myConfig, MyConfig.class);MyConfig myConfig2 applicationContext.getBean(myConfig, MyConfig.class);System.out.println(myConfig1 myConfig2);// 注意如果 MYConfig配置类没有按照规范编写则容器中bean 的name为 类名}
}SpringBoot 提供一个注解和当前注解功能一样SpringBootConfiguration 2、proxyBeanMethods代理bean的方法属性since spring 5.2以后
功能
proxyBeanMethods trueFull模式保证每个Bean方法被调用多少次返回的组件都是单实例的proxyBeanMethods falseLite模式每个Bean方法被调用多少次返回的组件都是新创建的
演示
默认 proxyBeanMethodstruespringBoot会检查这个组件是否在容器中有,有则直接引用
// 默认 proxyBeanMethodstrue springBoot会检查这个组件是否在容器中有,有则直接引用
User user3 myConfig1.getUser();
System.out.println(user1 user3); // true修改 proxyBeanMethodsfalse则每调用一次Spring就会创建一个新的Bean对象
在执行结果则为 false 证明两次获取的bean不是同一个bean。
小结
组件依赖必须使用Full模式默认。Full模式每次都会检查bean效率较Lite模式慢
04-SpringBoot2高级-底层原理-Import注解使用1
目的能够理解Import注解作用及4种使用方式
讲解
作用使用Import导入的类会被Spring加载到IOC容器中
Import提供4种用法
导入Bean导入配置类导入 ImportSelector 实现类。一般用于加载配置文件中的类导入 ImportBeanDefinitionRegistrar 实现类
实现
1、导入Bean
package com.itheima.sh;import com.itheima.sh.pojo.User;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;import java.util.Map;SpringBootApplication
Import(User.class)
//会自动执行当前类的构造方法创建对象存到IOC容器, bean名称为类的全路径public class DataApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext SpringApplication.run(DataApplication.class, args);MapString, User map applicationContext.getBeansOfType(User.class);System.out.println(map);User user1 applicationContext.getBean(com.itheima.sh.pojo.User, User.class);System.out.println(user1);}
}2、导入配置类
package com.itheima.sh;import com.itheima.sh.config.MyConfig;
import com.itheima.sh.pojo.User;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;import java.util.Map;SpringBootApplication
MapperScan(basePackages com.itheima.sh.mapper)
//Import(User.class)
//1、会自动执行当前类的构造方法创建对象存到IOC容器, bean名称为类的全路径Import(MyConfig.class)
//2、创建MyConfig bean并且类中有 带有Bean注解方法创建对象存到IOC容器bean名称为默认方法名称
public class DataApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext SpringApplication.run(DataApplication.class, args);//{getUsercom.itheima.sh.pojo.User1b4a3a1}MapString, User map applicationContext.getBeansOfType(User.class);System.out.println(map);User user1 applicationContext.getBean(getUser, User.class);System.out.println(user1);MapString, MyConfig config applicationContext.getBeansOfType(MyConfig.class);//{com.itheima.sh.config.MyConfigcom.itheima.sh.config.MyConfig7e848aea}System.out.println(config);}
}05-SpringBoot2高级-底层原理-Import注解使用2
目的讲解Import注解使用另外两种使用方式
步骤
导入 ImportSelector 实现类。一般用于加载配置文件中的类导入 ImportBeanDefinitionRegistrar 实现类
实现
导入 ImportSelector 实现类。一般用于加载配置文件中的类
1、编写 ImportSelector 实现类MyImportSelector
2、引导类导入
package com.itheima.sh;import com.itheima.sh.config.MyImportSelector;
import com.itheima.sh.pojo.User;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;import java.util.Map;SpringBootApplication
MapperScan(basePackages com.itheima.sh.mapper)
//Import(User.class)
//1、会自动执行当前类的构造方法创建对象存到IOC容器, bean名称为类的全路径//Import(MyConfig.class)
//2、创建MyConfig bean并且类中有 带有Bean注解方法创建对象存到IOC容器bean名称为默认方法名称Import(MyImportSelector.class)
//3、创建MyConfig bean 名称为类名全路径创建带有Bean注解方法实例名称为方法名称
public class DataApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext SpringApplication.run(DataApplication.class, args);MapString, MyConfig map applicationContext.getBeansOfType(MyConfig.class);//{com.itheima.sh.config.MyConfigcom.itheima.sh.config.MyConfig44384b4a}System.out.println(map);MapString, User userMap applicationContext.getBeansOfType(User.class);//{getUsercom.itheima.sh.pojo.User5cc3e49b}System.out.println(userMap);}
}导入 ImportBeanDefinitionRegistrar 实现类
1、编写 ImportBeanDefinitionRegistrar 实现类MyImportBeanDefinitionRegistrar
package com.itheima.sh.config;import com.itheima.sh.pojo.User;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {/*** param importingClassMetadata 导入类的元注解信息* param registry Bean注册表*/Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {AbstractBeanDefinition beanDefinition BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition();registry.registerBeanDefinition(user, beanDefinition);}
}2、引导类测试
package com.itheima.sh;import com.itheima.sh.config.MyImportBeanDefinitionRegistrar;
import com.itheima.sh.pojo.User;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;import java.util.Map;SpringBootApplication
MapperScan(basePackages com.itheima.sh.mapper)//Import(User.class)
//1、会自动执行当前类的构造方法创建对象存到IOC容器, bean名称为类的全路径//Import(MyConfig.class)
//2、创建MyConfig bean并且类中有 带有Bean注解方法创建对象存到IOC容器bean名称为默认方法名称//Import(MyImportSelector.class)
//3、创建MyConfig bean 名称为类名全路径创建带有Bean注解方法实例名称为方法名称Import(MyImportBeanDefinitionRegistrar.class)
//4、创建Bean名称在registerBeanDefinition中定义
public class DataApplication {public static void main(String[] args) {ConfigurableApplicationContext applicationContext SpringApplication.run(DataApplication.class, args);MapString, User userMap applicationContext.getBeansOfType(User.class);//{usercom.itheima.sh.pojo.User23c7cb18}System.out.println(userMap);}
}小结
讲解当前小节的目的主要是为源码准备还有我们也可以知道创建Bean对象还可以使用Import四种方式
06-SpringBoot2高级-底层原理-Conditional衍生条件装配
目的理解Conditional衍生条件装配的作用
讲解
作用条件装配满足Conditional指定的条件则进行组件注入初始化Bean对象到IOC容器 。 演示
在RedisConfig类中添加注释
方法中定义
类上定义
注意也可以添加到 类上 满足条件则类及类中的对象生效。
小结
ConditionalOnXXX 注解存在的意义是满足条件当前类或者Bean才有效按需导入。
07-SpringBoot2高级-底层原理-ConfigurationProperties配置绑定
目的
回顾 ConfigurationProperties配置绑定 存在的目的是获取配置属性或者是配置文件指定前缀的属性信息并且初始化Bean对象到 IOC 容器。
由此我们可以想将来的配置我们可以放在配置文件中通过这个注解来读取并封装成对象
08-SpringBoot2高级-自动化配置原理-SpringBootApplication入口分析
目的能够理解SpringBoot自动化配置流程中SpringBootApplication是一个组合注解及每一个注解的作用能够知道作用。
讲解
1、SpringBoot是一个组合注解
2、SpringBootConfiguration注解作用
SpringBootConfiguration是对Configuration注解的包装proxyBeanMethods 默认配置 true full模式单例Bean标识是一个配置类所以 引导类也是配置类
3、ComponentScan注解作用
组件扫描默认扫描的规则 引导类所在的包及其子包所有带注解的类
问题
在引导类中配置 Bean 注解可以吗为什么Controller、service类添加完注解后不需要添加扫描包
09-SpringBoot2高级-自动化配置原理-EnableAutoConfiguration自动配置注解
目的理解EnableAutoConfiguration自动化配置核心实现注解
讲解
1、EnableAutoConfiguration是一个组合注解
2、AutoConfigurationPackage注解作用
作用利用Registrar给容器中导入一系列组件
点击 Registrar 进入到源码的 register 方法添加 断点测试
通过 debug 程序发现默认情况下 将引导类的所有包及其子包的组件导入进来
3、Import(AutoConfigurationImportSelector.class)注解作用
作用是利用selectImports方法中的 getAutoConfigurationEntry 方法给容器中批量导入相关组件
调用流程分析 调用AutoConfigurationImportSelector类中的selectImports方法 调用ListString configurations getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类 利用工厂加载 MapString, ListString loadSpringFactories(Nullable ClassLoader classLoader)得到所有的组件 从META-INF/spring.factories位置来加载一个文件。 默认扫描我们当前系统里面所有META-INF/spring.factories位置的文件 spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories
通过这个配置文件加载的自动配置当前版本2.3.10是有127个默认的自动化配置
小结
自动化配置默认加载的配置文件在哪
10-SpringBoot2高级-自动化配置原理-按条件开启自动配置类和配置项
目的
能够理解所有的自动化配置虽然会全部加载底层有大量的ConditionalOnXXX有很多自动配置类并不能完全开启。如果配置生效了则会加载默认的属性配置类实现默认的对应场景的自动化配置
讲解
1、以上通过 META-INF/spring.factories 配置文件找到所有的自动化配置类但 是不是全部的生效的呢很显然是不可能全部都生效的。
2、以 JdbcTemplateAutoConfiguration 为例讲解 进入到 JdbcTemplateAutoConfiguration 自动化配置类。
//配置类Lite模式
Configuration(proxyBeanMethods false)
//存在 DataSource、JdbcTemplate 类时再加载当前类
ConditionalOnClass({DataSource.class, JdbcTemplate.class })//容器中只有一个指定的Bean或者这个Bean是首选Bean 加载当前类
ConditionalOnSingleCandidate(DataSource.class)//在配置类 DataSourceAutoConfiguration 之后执行
AutoConfigureAfter(DataSourceAutoConfiguration.class)//如果条件满足开始加载自动化配置的属性值 JdbcProperties
EnableConfigurationProperties(JdbcProperties.class)Import({JdbcTemplateConfiguration.class, NamedParameterJdbcTemplateConfiguration.class })
public class JdbcTemplateAutoConfiguration {}3、JdbcProperties用于加载默认的配置如果配置文件配置了该属性则配置文件就生效。
4、通过Import导入JdbcTemplateConfiguration
//配置类Lite模式
Configuration(proxyBeanMethods false)
//没有JdbcOperations类型的bean时加载当前类而 JdbcTemplate 是 JdbcOperations 接口实现类
ConditionalOnMissingBean(JdbcOperations.class)
class JdbcTemplateConfiguration {BeanPrimaryJdbcTemplate jdbcTemplate(DataSource dataSource, JdbcProperties properties) {JdbcTemplate jdbcTemplate new JdbcTemplate(dataSource);JdbcProperties.Template template properties.getTemplate();jdbcTemplate.setFetchSize(template.getFetchSize());jdbcTemplate.setMaxRows(template.getMaxRows());if (template.getQueryTimeout() ! null) {jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());}return jdbcTemplate;}}验证我们可以在我们自己的项目里面创建一个 JdbcTemplate Bean看容器创建的Bean执行的是哪一个方法。
Configuration
public class MyConfig {Beanpublic JdbcTemplate jdbcTemplate(DataSource dataSource){JdbcTemplate jdbcTemplate new JdbcTemplate(dataSource);System.out.println(自定义 JdbcTemplate);return jdbcTemplate;}
}结果保证容器中只有一个 Bean 实例
问题
这些不用的 starter 的依赖能不能导入到我们工程里面 为什么
11-SpringBoot2高级-自动化配置原理-debug全流程
目的能够理解整个SpringBoot启动的完成自动化配置及属性加载的全过程
12-SpringBoot2高级-自动化配置原理-总结
SpringBoot自动化配置流程总结
程序启动找到自动化配置包下 META-INF/spring.factories 的EnableAutoConfigurationSpringBoot先加载所有的自动配置类 xxxxxAutoConfiguration每个自动配置类按照条件进行生效。生效的配置类就会给容器中装配很多组件只要容器中有这些组件相当于这些功能就有了定制化配置 用户直接自己Bean替换底层的组件用户去看这个组件是获取的配置文件什么值就去修改。 #mermaid-svg-tnF0Ohae9vGferwq {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-tnF0Ohae9vGferwq .error-icon{fill:#552222;}#mermaid-svg-tnF0Ohae9vGferwq .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-tnF0Ohae9vGferwq .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-tnF0Ohae9vGferwq .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-tnF0Ohae9vGferwq .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-tnF0Ohae9vGferwq .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-tnF0Ohae9vGferwq .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-tnF0Ohae9vGferwq .marker{fill:#333333;stroke:#333333;}#mermaid-svg-tnF0Ohae9vGferwq .marker.cross{stroke:#333333;}#mermaid-svg-tnF0Ohae9vGferwq svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-tnF0Ohae9vGferwq .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-tnF0Ohae9vGferwq .cluster-label text{fill:#333;}#mermaid-svg-tnF0Ohae9vGferwq .cluster-label span{color:#333;}#mermaid-svg-tnF0Ohae9vGferwq .label text,#mermaid-svg-tnF0Ohae9vGferwq span{fill:#333;color:#333;}#mermaid-svg-tnF0Ohae9vGferwq .node rect,#mermaid-svg-tnF0Ohae9vGferwq .node circle,#mermaid-svg-tnF0Ohae9vGferwq .node ellipse,#mermaid-svg-tnF0Ohae9vGferwq .node polygon,#mermaid-svg-tnF0Ohae9vGferwq .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-tnF0Ohae9vGferwq .node .label{text-align:center;}#mermaid-svg-tnF0Ohae9vGferwq .node.clickable{cursor:pointer;}#mermaid-svg-tnF0Ohae9vGferwq .arrowheadPath{fill:#333333;}#mermaid-svg-tnF0Ohae9vGferwq .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-tnF0Ohae9vGferwq .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-tnF0Ohae9vGferwq .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-tnF0Ohae9vGferwq .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-tnF0Ohae9vGferwq .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-tnF0Ohae9vGferwq .cluster text{fill:#333;}#mermaid-svg-tnF0Ohae9vGferwq .cluster span{color:#333;}#mermaid-svg-tnF0Ohae9vGferwq div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-tnF0Ohae9vGferwq :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} xxxxAutoConfiguration Bean组件 xxxxProperties里面取值 application.properties 开发人员使用步骤总结
引入场景依赖查看自动配置了哪些选做 自己分析引入场景对应的自动配置一般都生效了配置文件中debugtrue开启自动配置报告。Negative不生效\Positive生效 自己分析是否需要修改参照文档修改配置项xxxxProperties绑定了配置文件的哪些。 自定义加入或者替换组件Bean、Component等