Spring Boot介绍
Spring Boot是Spring的一个成员,可以简化Spring,Spring MVC的使用,但是其核心还是IOC容器。
特点:
-
创建Spring项目
-
内嵌Tomcat,jetty or Undertow (默认的是Tomcat)
-
提供了starter起步的依赖,简化了应用的配置。比如使用MyBatis框架,需要时在Spring的项目中,配置MyBatis的对象SqlSessionFacotyr, Dao的代理对象。在Spring Boot项目中,在pom.xml里面,加入一个mybatis-spring-boot-starter依赖
-
尽可能的去配置Spring和第三方库
-
提供了健康检查,统计,外部化配置
-
不用生成代码,不使用XML,做配置。
Spring Boot项目创建
第一种方式
使用Spring提供的初始化器,就是向导创建Spring Boot
使用地址:https://start.spring.io
Spring boot项目的结构:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-e9xOIts3-1683705323259)(/Users/wangqiang/Library/Application Support/typora-user-images/image-20230509104806854.png)]
第二种方式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6YLzicf7-1683705323260)(/Users/wangqiang/Library/Application Support/typora-user-images/image-20230509112438642.png)]
使用地址(国内地址):https://start.springboot.io
SpringBoot 注解
@SpringBootApplication 复合注解 由以下三个注解组成
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
1. @SpringBootConfiguration
@Configuration
public @interface SpringBootConfiguration {
@AliasFor(
annotation = Configuration.class
)
boolean proxyBeanMethods() default true;
}
说明:使用@SpringBootConfiguration 注解的类,可以做为配置文件使用的,可以使用Bean声明对象,注入到容器。(因为使用@SpringBootConfiguration中用了@Configuration注解,@Configuration可以实现这个功能)
2.@EnableAutoConfiguration
启用自动的配置,把java对象配置好,注入到Spring容器中,例如可以把mybatis的对象创建好,放入到容器中。
3.@ComponentScan
扫描器,找到注解,根据注解的功能创建对象,给属性赋值等等。默认扫描的包:@ComponentScan所在类所在的包及子包当中。
Spring Boot 配置文件
配置文件名称:application
扩展名:
-
properties (key = value)
-
yml (key: value) :后有空格
# properties 扩展名
# 设置端口号
server.port=8082
#设置访问应用上下文的路径 contextpath
server.servlet.context-path=/myspringboot
访问浏览器地址变为:http://localhost:8082/myspringboot/hello
# yml 扩展名
server:
port: 8083
servlet:
context-path: /myspringboot2
访问浏览器地址变为:http://localhost:8083/myspringboot2/hello
多环境配置
有开发环境,测试环境,上线的环境
每个环境有不同的配置信息,例如端口,上线文件,数据库URL,用户名,密码等等。
使用多环境配置文件,可以方便 的切换不同的配置。
使用方式:创建多个配置文件,名称规则:application-环境名称.properties(yml)
创建测试者使用的配置:applicatio-test.properties
在application.properties 对不同的环境进行激活。使用别的环境时候别的环境进行注射。
# 激活使用那个配置文件
spring.profiles.active = test (环境名称)
注解@Value
@Value(“${key}”),key来自application。properties(yml)读取数据
@Value("${server.port}")
private Integer port;
@RequestMapping("/data")
@ResponseBody
public String queryDate(){
return port;
}
使用JSP
SpringBoot不推荐使用JSP,而是使用模版技术代替JSP、
使用jsp需要配置,加入一个处理jsp的依赖,负责编译jsp文件
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
如果需要使用servlet,jsp,jstl的功能
创建一个存放jsp的目录,一般叫做webapp index.jsp
需要在pom.xml文件jsp文件编译后的存放目录。META-INF/resources
创建Controller,访问JSP
在application.propertise文件中配置视图解析器
使用容器
通过代码,从容器当中获取对象
通过SpringApplication.run(SpringbootSrcondApplication.class, args);返回值获取容器。
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
return run(new Class[]{primarySource}, args);
}
ConfigurableApplicationContext 是一个接口。是ApplicationContext的子接口。
public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable
CommandLineRunner 接口,ApplicationRunnner接口
这两个接口都有一个run方法,执行时间在容器对象创建好后,自动执行run()方法。可以完成自定义的容器对象创建好的一些操作。