网页设计和网站建设实战大全,深圳学校网站建设,城关区建设局网站,交流平台网站架构怎么做文章目录原理初探主程序关于spring boot#xff0c;谈谈你的理解#xff1a;微服务阶段原理初探
pom.xml
spring-boot-dependencies#xff1a;核心依赖在父工程中#xff01;我们在写或者引入一些springboot依赖的时候#xff0c;不需要指定版本#xff0c;就因为有这…
文章目录原理初探主程序关于spring boot谈谈你的理解微服务阶段原理初探
pom.xml
spring-boot-dependencies核心依赖在父工程中我们在写或者引入一些springboot依赖的时候不需要指定版本就因为有这些版本仓库
启动器 parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.1.9.RELEASE/versionrelativePath/ !-- lookup parent from repository --
/parent进入父项目parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion2.1.9.RELEASE/versionrelativePath../../spring-boot-dependencies/relativePath
/parent启动器说白了就是Springboot的启动场景比如spring-boot-starter-web他就会帮我们自动导入web环境所有的依赖springboot会将所有场景都变成一个个启动器我们要是用什么功能就只需要找到对应的启动器就可以了 ‘starter’
主程序
// SpringBootApplication 标注这个类是一个springboot的应用
SpringBootApplication
public class HelloworldApplication {public static void main(String[] args) {SpringApplication.run(HelloworldApplication.class, args);}
}注解 SpringBootConfiguration springboot的配置Configuration spring 配置类Component 说明这也是一个spring的组件EnableAutoConfiguration : 自动配置AutoConfigurationPackage 自动配置包Import({AutoConfiguration.Registrar.class}): 自动配置‘包注册’Import({AutoConfigurationImportSelector.class}) 自动导入选择// 获取所有配置 ListString getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) 获取候选的配置 META-INF/spring.factories: 自动配置的核心文件 结论springboot中所有的自动配置都是在启动的时候扫描并加载但是不一定生效但是要判断条件是否成立只要导入了对应的start就有了对应的启动器了有了启动器我们的自动装配就会生效然后启动成功
springboot在启动的时候在类路径下 /META-INF/spring.factories 获取指定的值将这些自动配置的类导入容器自动配置就会生效帮我们进行自动配置以前我们需要自动配置的东西现在springboot帮我们做了整合javaEE解决方案和自动配置的东西都在spring-boot-autoconfigure:2.2.4.RELEASE.jar中他会把所有需要导入的组件以类名的方式返回这些组件就会被添加到容器中容器中也会存在非常多的XXXAutoConfiguration文件就是这些文件给容器中导入了这个场景需要的所有组件并自动配置ConfigurationJavaConfig有了自动配置类免去了我们手动编写配置文件的工作
关于spring boot谈谈你的理解
自动装配run() SpringApplication.run分析 分析该方法主要分两部分一部分是SpringApplication的实例化二是run方法的执行 SpringApplication 这个类主要做了以下四件事情 推断应用的类型是普通的项目还是Web项目查找并加载所有可用初始化器 设置到initializers属性中找出所有的应用程序监听器设置到listeners属性中推断并设置main方法的定义类找到运行的主类 查看构造器public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {this.sources new LinkedHashSet();this.bannerMode Mode.CONSOLE;this.logStartupInfo true;this.addCommandLineProperties true;this.addConversionService true;this.headless true;this.registerShutdownHook true;this.additionalProfiles new HashSet();this.isCustomEnvironment false;this.resourceLoader resourceLoader;Assert.notNull(primarySources, PrimarySources must not be null);this.primarySources new LinkedHashSet(Arrays.asList(primarySources));this.webApplicationType WebApplicationType.deduceFromClasspath();this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));this.mainApplicationClass this.deduceMainApplicationClass();
}run方法
全面接管SpringMVC的配置实操