文章目录
- 写在前面
- 官网上是这么去描述springboot以及总结springboot特点的
- springBoot特点
- spring核心流程简图
- Springboot常用注解
- springBoot自动装配原理
- 手写如何自定义starter
- SpringBoot是如何启动tomcat的
写在前面
springBoot官网
官网上是这么去描述springboot以及总结springboot特点的
Spring Boot makes it easy to create stand-alone, production-grade
Spring based Applications that you can “just run”.We take an opinionated view of the Spring platform and third-party
libraries so you can get started with minimum fuss. Most Spring Boot
applications need minimal Spring configuration.
springBoot可以轻松的创建一个独立的,生产级别的,基于spring应用的,你可以立即运行的程序。
对于这句话的理解对于ssm的老开发,感受会非常深,因为需要配置一系列bean,比如,使用springmvc,我们需要配置dispatcherServlet,handlermapping,hadlerAdapter,viewResolver。使用到数据库,需要配置dataSource,JdbcTemplate,使用mybatis,需要配置SqlSessionFactoryBean会话工厂。。
我们对Spring平台和第三方库持有一种有立场的观点,这样您就可以尽可能轻松地开始。大多数Spring Boot应用程序只需要最少的Spring配置。
springBoot特点
- 创建一个独立的spring应用
- 直接内嵌了tomcat,jetty or Undertow (不需要不是war)
- 提供约定好的starter依赖项,去简化构建配置
- 尽可能自动配置spring及第三方依赖包
- 提供生产就绪的功能,例如指标、运行状况检查和外部化配置
- 完全不需要生成代码,也不需要 XML 配置
spring核心流程简图
- SpringBoot通过**@EnableAutoConfiguration注解开启自动配置**,加载spring.factories中注册的各种AutoConfiguration类
- 当某个AutoConfiguration类满足其注解@Conditional指定的生效条件时,实例化该AutoConfiguration类中定义的Bean(组件等),并注入Spring容器/Springboot容器里,就可以完成依赖框架的自动配置。
Springboot常用注解
@SpringBootApplication 启动类上的注解,他是一个复合注解,下面主要介绍三个注解
- @SpringBootConfiguration
- 这个注解实际就是一个@Configuration,表示启动类也是一个配置类
- @ComponentScan
- 表示扫描路径,因为默认是没有配置实际扫描路径的,所以SpringBoot扫描的路径是启动类所在的包以及子包
- @EnableAutoConfiguration
- @EnableAutoConfiguration的关键功能是通过**@Import注解导入的AutoConfigurationImportSelector** 来完成从spring.factories 中加载配置类。
springBoot自动装配原理
- 通过**@EnableAutoConfiguration** 的 @Import注解导入的AutoConfigurationImportSelector 扫描所有包的META-INF目录下的来完成从spring.factories 中加载配置类到一个String数组中。
- 交给Spring, Spring会将它们封装成BeanDefinition,放到BeanDefinitionMap中去,最后Spring就能管理到这些Bean
手写如何自定义starter
创建配置类,@Configuration配置类,还可以添加一些@ConditionOnXXX的注解控制配置的生效条件,结合XXXProperties 获取配置信息 等。
@Configuration
@EnableConfigurationProperties(UserProperties.class)
@ConditionalOnClass(ChinService.class)
public class LisAutoConfiguration {@Autowiredpublic UserProperties userProperties;@Bean@ConditionalOnMissingBean(name = "chinService")public ChinService chinService(){return new ChinService(userProperties);}
}
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.hbwxz.learnspringboot.autoConfigure.LisAutoConfiguration
创建UserProperties读取配置文件
package com.hbwxz.learnspringboot.config;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "ls")
public class UserProperties {private String username;private Integer age;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}
创建服务类,也就是我们真正应用需要注入的bean
package com.hbwxz.learnspringboot.service;import com.hbwxz.learnspringboot.config.UserProperties;public class ChinService {private UserProperties userProperties;public ChinService(UserProperties userProperties) {this.userProperties = userProperties;}public void getUserProperties() {System.out.println(userProperties.getAge());}public UserProperties getUser() {return this.userProperties;}}
在另一个项目中引用这个starter依赖并测试
配置文件
ls:username: akaage: 10
测试类
@SpringBootTest
@RunWith(SpringRunner.class)
public class testStarter {@AutowiredChinService chinService;@Testpublic void test1(){chinService.getUserProperties();}
}
控制台
2024-03-26 17:17:27.703 INFO 24256 --- [ main] com.test.starter.testStarter : Starting testStarter using Java 1.8.0_221 on DESKTOP-C8H6MAH with PID 24256 (started by admin in D:\code\springboot\testStarter)
2024-03-26 17:17:27.704 INFO 24256 --- [ main] com.test.starter.testStarter : No active profile set, falling back to 1 default profile: "default"
2024-03-26 17:17:28.936 INFO 24256 --- [ main] com.test.starter.testStarter : Started testStarter in 1.566 seconds (JVM running for 2.313)
10
SpringBoot是如何启动tomcat的
- ⾸先SpringBoot在启动时会先创建⼀个Spring容器
- 再创建容器的onRefresh方法中会创建webServer
- 通过new Tomcat对象,绑定端口及协议,然后去启动tomcat服务器