SpringBoot
应用场景
- 传统Web MVC架构
- 前后端分离架构
- 微服务架构
特性
- 可创建独立的Spring应用程序
- 提供嵌入式Web Server(无需部署war包)
- 无需任何代码生成技术也无任何XML配置
- 自动化配置
- 提供一系列生产级特性
- 端点(
endpoints.enable=true开启端点)autoconfig获取自动配置信息beans获取Spring Bean基本信息configprops获取配置项信息dump获取当前线程基本信息env获取环境变量信息health获取健康检查信息enpoints.health.enable = true开启健康检查enpoints.health.sensitive = true获取敏感性,默认false,如果为true,则不返回敏感字段enpoints.health.time-to-live = 500设置健康检查的缓存时间,单位ms,默认1000- 自定义健康检查器
- 实现org.springframework.boot.actuate.health.HealthIndicator接口
info获取应用基本信息enpoints.info.enable = true开启应用信息info.app.name = @project.name@获取pom.xml中的nameinfo.app.description = @project.description@获取pom.xml中的descriptioninfo.app.version = @project.version@获取pom.xml中的version- 可通过git-commit-id-plugin插件获取Git信息
info.git.branch = @git.branch@获取Git版本信息info.git.commit.id = @git.commit.id@获取Git提交IDinfo.git.commit.time = @git.commit.time@获取Git提交时间
metrics获取性能指标信息enpoints.metrics.enable = true开启性能指标enpoints.metrics.id = performance自定义IDenpoints.metrics.path = /enpoints/metrices自定义请求路径
mappings获取请求映射信息trace获取请求调用信息
- 跨域(Spring4.2之后支持)
- 方法一
endpoints.cors.allowed-origins = http://www.baidu.com配置跨域域名endpoints.cors.allowed-methods = GET,POST,PUT,DELETE配置跨域支持方法
- 方法二
- @CrossOrigin注解
- 方法三
- 继承org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter的addCorsMappings方法
- 方法一
- 外部化配置,按顺序如下
- Java命令行参数
- JNDI属性
- Java系统属性
- 操作系统环境变量
- Jar包外的application.properties配置
- Jar包内的application.properties配置
- @PropertySource注解
- SpringApplication.setDefaultProperties默认值
- 远程监控
- ssh方式
- 引入spring-boot-starter-remote-shell
- 配置
- shell.ssh.port = 2000
- shell.auth.simple.user.name = admin
- shell.auth.simple.user.password = 123456
- ssh -p 2000 admin@127.0.0.1
- telnet方式
- 引入crsh.shell.telnet
- ssh方式
- 端点(
- 提供开箱即用的Spring插件
@EnableWebMvc`
@EnableAsync
@EnableAspectJAutoProxy
处理类
ConfigurationClassParser扫描类
ClassPathBeanDefinitionScannerClassPathScanningCandidateComponentProvider
注解驱动上下文
AnnotationConfigApplicationContext
Spring 事件
ContextRefreshedEventApplicationContextEventApplicationEvent
ContextClosedEvent
自定义事件
PayloadApplicationEvent
Spring 事件的类型 ApplicationEvent
Spring 事件监听器 ApplicationListener
Spring 事件广播器 ApplicationEventMulticaster
- 实现类:
SimpleApplicationEventMulticaster
Spring事件执行顺序
- ApplicationStartingEvent(1)
- ApplicationEnvironmentPreparedEvent(2)
- ApplicationPreparedEvent(3)
- ContextRefreshedEvent
- ServletWebServerInitializedEvent
- ApplicationStartedEvent(4)
- ApplicationReadyEvent(5)
- ContextClosedEvent
- ApplicationFailedEvent (特殊情况)(6)
优化
- 移除JspServlet(Jsp页面编译)
- 移除DefaultServlet(静态资源解析)