SpringBoot是Spring中的一个成员, 可以简化Spring,SpringMVC的使用。 他的核心还是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.2 创建Spring Boot项目
2.2.1 第一种方式, 使用Spring提供的初始化器, 就是向导创建SpringBoot应用
使用的地址: https://start.spring.io
SpringBoot项目的结构:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MHen1vgX-1674540063103)(D:\course\25-SpringBoot\笔记\images\image-20210115152427829.png)]
2.2.1 使用国内的地址
https://start.springboot.io
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TN0IhrVH-1674540063105)(D:\course\25-SpringBoot\笔记\images\image-20210115155556662.png)]
2.3 注解的使用
@SpringBootApplication
符合注解:由
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
1.@SpringBootConfiguration
@Configuration
public @interface SpringBootConfiguration {
@AliasFor(
annotation = Configuration.class
)
boolean proxyBeanMethods() default true;
}
说明:使用了@SpringBootConfiguration注解标注的类,可以作为配置文件使用的,
可以使用Bean声明对象,注入到容器
2.@EnableAutoConfiguration
启用自动配置, 把java对象配置好,注入到spring容器中。例如可以把mybatis的对象创建好,放入到容器中
3.@ComponentScan
@ComponentScan 扫描器,找到注解,根据注解的功能创建对象,给属性赋值等等。
默认扫描的包: @ComponentScan所在的类所在的包和子包。
2.4 SpringBoot的配置文件
配置文件名称: application
扩展名有: properties( k=v) ; yml ( k: v)
使用application.properties, application.yml
例1:application.properties设置 端口和上下文
#设置端口号
server.port=8082
#设置访问应用上下文路径, contextpath
server.servlet.context-path=/myboot
例2: application.yml
server:
port: 8083
servlet:
context-path: /myboot2