1. SpringBoot产生原因
- spring, springmvc框架使用上的一些缺点:
- 【SpringBoot 02: 初识SpringBoot】需要使用的大量的配置文件
- 还需要配置各种对象
- 需要把使用的对象放入到spring容器中才能使用对象
- 需要了解其他框架配置规则
- springboot的一些直观优点:
- SpringBoot就相当于简化了配置文件的Spring+SpringMVC(但springboot的核心还是IOC容器)
- 常用的框架和第三方库都已经配置好了, 只需要引入使用即可
- Create stand-alone Spring applications
可以创建spring应用
- Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
内嵌的tomcat, jetty, Undertow
- Provide opinionated 'starter' dependencies to simplify your build configuration
提供了starter起步依赖, 简化应用的配置:比如使用MyBatis框架, 需要在Spring项目中, 需要配置MyBatis的对象, SqlSessionFactory以及Dao的代理对象但在SpringBoot项目中, 只要在pom.xml里面加入一个mybatis-spring-boot-starter依赖
- Automatically configure Spring and 3rd party libraries whenever possible
尽可能去自动配置spring和第三方库, 叫做自动配置(就是把spring中的,第三方库中的对象都创建好,放到容器中,开发人员可以直接使用)
- Provide production-ready features such as metrics, health checks, and externalized configuration
提供了健康检查, 统计, 外部化配置
- Absolutely no code generation and no requirement for XML configuration
不用生成代码,不需要使用xml文件做配置
2. SpringBoot项目地址- 创建springboot项目时,可能用到的地址
- 国外地址: https://start.spring.io
- 国内地址: https://start.springboot.io
- 位于项目启动类上面,是复合注解, 包含以下注解
@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan
- 而@SpringBootConfiguration又是包含@Configuration的符合注解
@Configurationpublic @interface SpringBootConfiguration {@AliasFor(annotation = Configuration.class)boolean proxyBeanMethods() default true;}//说明使用了@SpringBootConfiguration注解标注的类,可以作为配置文件使用的,可以使用Bean声明对象,注入到容器
- @EnableAutoConfiguration
启用自动配置,把java对象配置好,注入到spring容器中 。例如可以把mybatis的对象创建好,放入到容器中
- @ComponentScan
扫描器,找到注解,根据注解的功能创建对象,给属性赋值等等 。默认扫描的包:@ComponentScan所在的类,以及其所在类所在的包和子包 。
SpringBoot的配置文件- 名称:application
- 后缀:property(key=value) 或 yml(key:value)
- 配置文件示例:
- 例1:application.properties设置端口和上下文
#设置端口号server.port=9090#设置访问应用上下文路径,contextpathserver.servlet.context-path=/myboot
- 例2:application.yml,配置文件的结构更加清晰,推荐使用
server:port: 9090servlet:context-path:/myboot
多环境配置文件- 实际场景中,项目的配置会有开发环境,测试环境,上线的环境
- 每个环境有不同的配置信息,例如端口,上下文,数据库url,用户名,密码等等
- 使用多环境配置文件,可以方便的切换不同的配置
- 使用方式:创建多个配置文件,名称规则:application-环境名称.properties(或者后缀未yml格式)
经验总结扩展阅读
- 超详细 SpringBoot 整合 Elasticsearch .md
- SpringBoot 01: JavaConfig + @ImportResource + @PropertyResource
- 第2-1-5章 docker安装MinIO实现文件存储服务-springboot整合minio-minio全网最全的资料
- 上 我服了!SpringBoot升级后这服务我一个星期都没跑起来!
- 第2-1-4章 SpringBoot整合FastDFS文件存储服务
- SpringBoot 自动装配原理
- springboot整合项目-商城个人头像上传功能
- springboot项目整合-注册功能模块开发
- SpringBoot 过滤器和拦截器
- SpringBoot 常用注解的原理和使用