Springboot是什么
springboot是什么
个人认为Springboot是 Spring的升级优化,SpringBoot省去一些经常重复的一样的框架搭建配置工作,提高开发效率,本质是Spring + 其他框架。
Springboot之前,很常见SSM和SSH的框架系统,本人在工作中初期系统也是这些框架搭建
SSM一般指SpringMVC+Spring+mybatis,
SSH 一般指Structs+Spring+Hibernate开发中系统可能会有其他扩展,如数据库连接,mysql,oracle,连接池配置,非关系型数据库,MongoDB,Redis,RPC连接服务调用dubbo ,消息队列 activemq,kafka等,这些都需要整合配置,很多时候可能十个系统业务不同,九个系统的架构都是SSM,核心一直都是spring,所以开发做多了很容易想到做一个架子模板,搭建十个系统都用它,只是业务不同,搭建好架子后主要是写业务代码;
springboot至少有这样的理由出现,提高开发效率,并且可以根据业务不同配置各种个性化。
原先一个SpringMVC + Spring + Mybatis的雏形框架搭建可能要一个小时搭好,有了SpringBoot快的话几分钟能搭好,并且内置Tomcat,可以启动服务。
springboot可以很方便的"一键开发部署",他整合了很多框架,提高开发效率,所以spring boot主要做的就是将各种功能组件整合,并且有默认配置;一般使用maven一步就能构建springboot项目
pom.xml 基本配置 maven3.6.1
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.domoment</groupId>
<artifactId>httpconcurrent</artifactId>
<version>0.0.1</version>
<name>httpconcurrent</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
springboot项目只需要一个注解和main方法就可以启动
@SpringBootApplication //一个注解
public class TesApplication {
public static void main(String[] args) { //main方法启动
SpringApplication.run(TesApplication.class, args);
}
}
这里运行main方法就可以启动一个springboot项目,并且启动了tomcat服务器(默认),原理需要从org.springframework.boot.SpringApplication入手
其他
SpringApplication
org.springframework.boot.SpringApplication是springboot项目的关键启动类
SpringApplication在运行main方法的时候其实做了很多,包括读取配置文件,校验,加载类,反射创建对象,初始化等,看看下面代码,SpringApplication.run()方法做了什么
//SpringApplication.run(TesApplication.class, args); -->
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return new SpringApplication(primarySources).run(args);
}
总表面看是创建了一个SpringApplication对象,然后调用对象的run方法
构造方法
// -->
// 构造方法
@SuppressWarnings({ "unchecked", "rawtypes" })
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
构造方法主要是做一些基本配置,spring.factories的读取,类文件的加载校验,基本初始化类初始化,监听器的初始化,容器类型的指定
非静态run方法
// SpringApplication对象的run方法 非静态
/**
* Run the Spring application, creating and refreshing a new
* {@link ApplicationContext}.
* @param args the application arguments (usually passed from a Java main method)
* @return a running {@link ApplicationContext}
*/
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
可以看到,run方法代码内容还是挺多的,通过字面理解,这里基本是大的方向的定义,
从参数读取->配置环境读取->忽略bean对象信息读取->banner图片输出->创建applicationContext->
......
->refreshContext 启动容器 ->
.....listenner.running(context)启动后的一些配置
tomcat在refreshContext中创建启动
一般springboot的tomcat创建启动是在refreshContext方法中被执行

浙公网安备 33010602011771号