动态电子商务网站 制作,怎么做个人网站建设,c4d培训,地产网站方案一、SpringBoot是什么#xff1f;
springboot是依赖于spring的#xff0c;比起spring#xff0c;除了拥有spring的全部功能以外#xff0c;springboot无需繁琐的xml配置#xff0c;这取决于它自身强大的自动装配功能#xff1b;并且自身已嵌入Tomcat、Jetty等web容器
springboot是依赖于spring的比起spring除了拥有spring的全部功能以外springboot无需繁琐的xml配置这取决于它自身强大的自动装配功能并且自身已嵌入Tomcat、Jetty等web容器集成了springmvc使得springboot可以直接运行不需要额外的容器提供了一些大型项目中常见的非功能性特性如嵌入式服务器、安全、指标健康检测、外部配置等。
springboot 是一个服务于框架的框架简化了spring的配置。
二、SpringBoot启动过程
启动流程图如下 1、运行 SpringApplication.run() 方法
可以肯定的是所有的标准的springboot的应用程序都是从run方法开始的
package com.spring;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;SpringBootApplication
public class App {public static void main(String[] args) {// 启动springbootConfigurableApplicationContext run SpringApplication.run(App.class, args);}}
进入run方法后会 new 一个SpringApplication 对象创建这个对象的构造函数做了一些准备工作编号第2~5步就是构造函数里面所做的事情
/*** Static helper that can be used to run a {link SpringApplication} from the* specified sources using default settings and user supplied arguments.* param primarySources the primary sources to load* param args the application arguments (usually passed from a Java main method)* return the running {link ApplicationContext}*/public static ConfigurableApplicationContext run(Class?[] primarySources,String[] args) {return new SpringApplication(primarySources).run(args);}
另外在说明一下springboot启动有三种方式.
2、确定应用程序类型
在SpringApplication的构造方法内首先会通过 WebApplicationType.deduceFromClasspath() 方法判断当前应用程序的容器默认使用的是Servlet 容器除了servlet之外还有NONE 和 REACTIVE 响应式编程 3、加载所有的初始化器
这里加载的初始化器是springboot自带初始化器从从 META-INF/spring.factories 配置文件中加载的那么这个文件在哪呢自带有2个分别在源码的jar包的 spring-boot-autoconfigure 项目 和 spring-boot 项目里面各有一个 spring.factories文件里面看到开头是 org.springframework.context.ApplicationContextInitializer 接口就是初始化器了 当然我们也可以自己实现一个自定义的初始化器实现 ApplicationContextInitializer接口既可
MyApplicationContextInitializer.java
package com.spring.application;import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
/*** 自定义的初始化器*/
public class MyApplicationContextInitializer implements ApplicationContextInitializerConfigurableApplicationContext {Overridepublic void initialize(ConfigurableApplicationContext configurableApplicationContext) {System.out.println(我是初始化的 MyApplicationContextInitializer...);}
}
在resources目录下添加 META-INF/spring.factories 配置文件内容如下将自定义的初始化器注册进去
org.springframework.context.ApplicationContextInitializer\
com.spring.application.MyApplicationContextInitializer 启动springboot后就可以看到控制台打印的内容了在这里我们可以很直观的看到它的执行顺序是在打印banner的后面执行的 4、加载所有的监听器
加载监听器也是从 META-INF/spring.factories 配置文件中加载的与初始化不同的是监听器加载的是实现了 ApplicationListener 接口的类 自定义监听器也跟初始化器一样依葫芦画瓢就可以了这里不在举例
5、设置程序运行的主类
deduceMainApplicationClass(); 这个方法仅仅是找到main方法所在的类为后面的扫包作准备deduce是推断的意思所以准确地说这个方法作用是推断出主方法所在的类 6、开启计时器
程序运行到这里就已经进入了run方法的主体了第一步调用的run方法是静态方法那个时候还没实例化SpringApplication对象现在调用的run方法是非静态的是需要实例化后才可以调用的进来后首先会开启计时器这个计时器有什么作用呢顾名思义就使用来计时的嘛计算springboot启动花了多长时间关键代码如下
// 实例化计时器
StopWatch stopWatch new StopWatch();
// 开始计时
stopWatch.start();
run方法代码段截图 7、将java.awt.headless设置为true
这里将java.awt.headless设置为true表示运行在服务器端在没有显示器器和鼠标键盘的模式下照样可以工作模拟输入输出设备功能。
做了这样的操作后,SpringBoot想干什么呢?其实是想设置该应用程序,即使没有检测到显示器,也允许其启动.对于服务器来说,是不需要显示器的,所以要这样设置.
方法主体如下
private void configureHeadlessProperty() {System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, System.getProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless)));}
通过方法可以看到setProperty()方法里面又有个getProperty()这不多此一举吗其实getProperty()方法里面有2个参数 第一个key值第二个是默认值意思是通过key值查找属性值如果属性值为空则返回默认值 true保证了一定有值的情况
8、获取并启用监听器
这一步 通过监听器来实现初始化的的基本操作这一步做了2件事情
创建所有 Spring 运行监听器并发布应用启动事件启用监听器 9、设置应用程序参数
将执行run方法时传入的参数封装成一个对象 仅仅是将参数封装成对象没啥好说的对象的构造函数如下
public DefaultApplicationArguments(String[] args) {Assert.notNull(args, Args must not be null);this.source new Source(args);this.args args;}
那么问题来了这个参数是从哪来的呢其实就是main方法里面执行静态run方法传入的参数 10、准备环境变量
准备环境变量包含系统属性和用户配置的属性执行的代码块在 prepareEnvironment 方法内 打了断点之后可以看到它将maven和系统的环境变量都加载进来了 11、忽略bean信息
这个方法configureIgnoreBeanInfo() 这个方法是将 spring.beaninfo.ignore 的默认值值设为true意思是跳过beanInfo的搜索其设置默认值的原理和第7步一样
private void configureIgnoreBeanInfo(ConfigurableEnvironment environment) {if (System.getProperty(CachedIntrospectionResults.IGNORE_BEANINFO_PROPERTY_NAME) null) {Boolean ignore environment.getProperty(spring.beaninfo.ignore,Boolean.class, Boolean.TRUE);System.setProperty(CachedIntrospectionResults.IGNORE_BEANINFO_PROPERTY_NAME,ignore.toString());}}
当然也可以在配置文件中添加以下配置来设为false
spring.beaninfo.ignorefalse
目前还不知道这个配置的具体作用待作者查明后在补上
12、打印 banner 信息
显而易见这个流程就是用来打印控制台那个很大的spring的banner的 那他在哪里打印的呢他在 SpringBootBanner.java 里面打印的这个类实现了Banner 接口
而且banner信息是直接在代码里面写死的 有些公司喜欢自定义banner信息如果想要改成自己喜欢的图标该怎么办呢其实很简单,只需要在resources目录下添加一个 banner.txt 的文件即可txt文件内容如下
_ _(_) | |_ _ _____ ___ _ __ __| | ___ _ __ __ _
| | | |/ _ \ \/ / | _ \ / _ |/ _ \| _ \ / _ |
| |_| | __/ | | | | | (_| | (_) | | | | (_| |\__, |\___/_/\_\_|_| |_|\__,_|\___/|_| |_|\__, |__/ | __/ ||___/ |___/
:: yexindong::
一定要添加到resources目录下别加错了 只需要加一个文件即可其他什么都不用做然后直接启动springboot就可以看到效果了 13、创建应用程序的上下文
实例化应用程序的上下文 调用 createApplicationContext() 方法这里就是用反射创建对象没什么好说的 14、实例化异常报告器
异常报告器是用来捕捉全局异常使用的当springboot应用程序在发生异常时异常报告器会将其捕捉并做相应处理在spring.factories 文件里配置了默认的异常报告器 需要注意的是这个异常报告器只会捕获启动过程抛出的异常如果是在启动完成后在用户请求时报错异常报告器不会捕获请求中出现的异常 了解原理了接下来我们自己配置一个异常报告器来玩玩
MyExceptionReporter.java 继承 SpringBootExceptionReporter 接口
package com.spring.application;import org.springframework.boot.SpringBootExceptionReporter;
import org.springframework.context.ConfigurableApplicationContext;public class MyExceptionReporter implements SpringBootExceptionReporter {private ConfigurableApplicationContext context;// 必须要有一个有参的构造函数否则启动会报错MyExceptionReporter(ConfigurableApplicationContext context) {this.context context;}Overridepublic boolean reportException(Throwable failure) {System.out.println(进入异常报告器);failure.printStackTrace();// 返回false会打印详细springboot错误信息返回true则只打印异常信息 return false;}
}
在 spring.factories 文件中注册异常报告器
# Error Reporters 异常报告器
org.springframework.boot.SpringBootExceptionReporter\
com.spring.application.MyExceptionReporter 接着我们在application.yml 中 把端口号设置为一个很大的值这样肯定会报错
server:port: 80828888
启动后控制台打印如下图 15、准备上下文环境
这里准备的上下文环境是为了下一步刷新做准备的里面还做了一些额外的事情 15.1、实例化单例的beanName生成器
在 postProcessApplicationContext(context); 方法里面。使用单例模式创建 了BeanNameGenerator 对象其实就是beanName生成器用来生成bean对象的名称
15.2、执行初始化方法
初始化方法有哪些呢还记得第3步里面加载的初始化器嘛其实是执行第3步加载出来的所有初始化器实现了ApplicationContextInitializer 接口的类
15.3、将启动参数注册到容器中
这里将启动参数以单例的模式注册到容器中是为了以后方便拿来使用参数的beanName 为 springApplicationArguments
16、刷新上下文
刷新上下文已经是spring的范畴了自动装配和启动 tomcat就是在这个方法里面完成的还有其他的spring自带的机制在这里就不一一细说了 17、刷新上下文后置处理
afterRefresh 方法是启动后的一些处理留给用户扩展使用目前这个方法里面是空的
/*** Called after the context has been refreshed.* param context the application context* param args the application arguments*/protected void afterRefresh(ConfigurableApplicationContext context,ApplicationArguments args) {}
18、结束计时器
到这一步springboot其实就已经完成了计时器会打印启动springboot的时长 在控制台看到启动还是挺快的不到2秒就启动完成了 19、发布上下文准备就绪事件
告诉应用程序我已经准备好了可以开始工作了 20、执行自定义的run方法
这是一个扩展功能callRunners(context, applicationArguments) 可以在启动完成后执行自定义的run方法有2中方式可以实现
实现 ApplicationRunner 接口实现 CommandLineRunner 接口
接下来我们验证一把为了方便代码可读性我把这2种方式都放在同一个类里面
package com.spring.init;import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;/*** 自定义run方法的2种方式*/
Component
public class MyRunner implements ApplicationRunner, CommandLineRunner {Overridepublic void run(ApplicationArguments args) throws Exception {System.out.println( 我是自定义的run方法1实现 ApplicationRunner 接口既可运行 );}Overridepublic void run(String... args) throws Exception {System.out.println( 我是自定义的run方法2实现 CommandLineRunner 接口既可运行 );}
}
启动springboot后就可以看到控制台打印的信息了