main() 方法启动,并通过 SpringApplication.run() 引导应用启动。SpringApplication 对象,推断应用类型、设置初始化器、设置启动监听器、确定主应用类。CommandLineRunner 和 ApplicationRunner 接口的初始化逻辑。ApplicationReadyEvent,应用进入运行状态,处理业务请求或任务。Spring Boot 是一个简化 Spring 应用程序开发的框架。它的主要目标是减少 Spring 应用程序的配置和开发复杂性,使我们能够更快地构建、测试和部署 Spring 应用。
@EnableAutoConfiguration),根据项目中的类路径依赖、环境变量等自动为应用配置适当的 Spring 模块,避免了大量的 XML 配置。java -jar 方式启动,而不需要部署到外部的 Web 服务器中。Spring Boot 的自动配置是通过 @EnableAutoConfiguration 注解实现,这个注解包含@Import({AutoConfigurationImportSelector.class})注解,导入的这个类会去扫描 classpath 下所有的 META-INF/spring.factories 中的文件,根据文件中指定的配置类加载相应的 Bean 的自动配置。
1)Tomcat(默认):Tomcat 是一种轻量级、广泛使用的 Servlet 容器
2)Jetty:Jetty 是一个高效的 Web 服务器和 Servlet 容器,通常用于嵌入式系统或对资源占用较敏感的环境。它比 Tomcat 更轻量,并且适合长连接应用(如 WebSocket、Comet)。
3)Undertow:Undertow 是一个轻量级的高性能 Web 服务器和 Servlet 容器,适合用于处理高并发的 HTTP 请求。它支持异步 IO 和 HTTP/2,是一种灵活、性能出色的选择。
4)Netty(仅限 WebFlux):对于使用 Spring WebFlux 的响应式 Web 应用,Spring Boot 支持 Netty 作为嵌入式 Web 容器。Netty 是一个非阻塞的异步事件驱动框架,非常适合响应式编程模型和高并发应用。
它们两者的区别就在于书写格式,对配置而言效果是一样的,就是个人偏好问题。默认优先级为:application.properties > application.yml
# application.properties
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=root
spring.datasource.password=secret# application.yml
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/db
username: root
password: secretmy:
custom:
property: 12341)使用 @Value 注解:
@Value("${my.custom.property}")
private String myProperty;2)使用 @ConfigurationProperties 注解:
@Component
@ConfigurationProperties(prefix = "my.custom")
public class MyCustomProperties {
private String property;
}3)使用 Environment 接口:
@Autowired
private Environment env;
public void someMethod() {
String value = env.getProperty("my.custom.property");
}简单优先级:
application-{profile}.properties > application.properties > application.ymlSpringBoot 默认是 Tomcat 这个 web 容器,实际上是这取决于 tomcat 的线程池配置。
tomcat 的默认核心线程数是 10,最大线程数 200,队列长度无限。但是它的线程池机制和 JDK 默认线程池不一样,因为主要是 I/O 密集型的任务,所以 tomcat 改造了线程池策略,在核心线程数满了之后,会直接创建线程到最大线程数。
所以,在默认的配置下,同一时刻,可以处理 200 个请求。 其他的请求只能排队了。
可以通过 server.tomcat.max-threads 修改最大最大工作线程数。
在 Spring Boot 中,Starter 是一组有用的依赖集合,用于简化构建配置。Spring Boot 通过 Starter POMs 提供了各种常用功能模块的集成,开发者只需引入一个 Starter 依赖,便可以自动获得该模块的所有相关依赖和配置,而无需手动添加多个依赖库和配置文件。
拦截器可以用来进行权限校验、日志记录、处理异常等问题
1)创建拦截器类:实现 HandlerInterceptor 接口,并实现接口中的方法,方法里面包括了几个请求时间点:请求前、请求后 、整个请求结束后(用于资源清理等操作)。
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("请求开始前");
return true; // 返回 true 继续处理,false 则拦截请求
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("请求后");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception {
System.out.println("请求完成");
}
}2)注册拦截器:通过实现 WebMvcConfigurer 的 addInterceptors 方法来添加自定义的拦截器。
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/api/**"); // 拦截指定路径
}
}Spring Boot 的事件机制,实际上是基于 Spring 的事件机制实现的,通过发布-订阅模式(使用观察者设计模式),主要用于应用程序中各个组件之间进行消息传递和解耦。
ApplicationEvent,或者任何自定义对象(从 Spring 4.2 开始,事件不再局限于 ApplicationEvent,可以是任意对象)ApplicationEventPublisher@EventListener 注解创建事件监听器。1)创建事件类
public class MyCustomEvent extends ApplicationEvent {
private final String message;
public MyCustomEvent(Object source, String message) {
super(source);
this.message = message;
}
public String getMessage(){
return message;
}
}2)发布事件
@Service
public class MyEventPublisher {
@Autowired
private ApplicationEventPublisher eventPublisher;
public void publishEvent(String message) {
MyCustomEvent event = new MyCustomEvent(this, message);
eventPublisher.publishEvent(event);
}
}3)监听事件
@Component
public class MyCustomListener {
@EventListener
public void handleCustomEvent(MyCustomEvent event) {
System.out.println("监听到消息 - " + event.getMessage());
}
}4)测试
@SpringBootTest
class TestSpringBootApplicationTests {
@Autowired
private MyEventPublisher myEventPublisher;
@Test
void contextLoads() {
myEventPublisher.publishEvent("哈喽!");
myEventPublisher.publishEvent("你好!");
}
}监听到消息 - 哈喽!
监听到消息 - 你好!主要有四种方式来实现异步处理: