SpringBoot 学习笔记
SpringBoot 学习笔记
一、SpringBoot 介绍
SpringBoot 是由 Pivotal 团队研发的,SpringBoot 并不是一门新技术,只是将之前常用的 Spring,SpringMVC,data-jpa 等框架封装到了一起,只是帮助你隐藏这些框架的整合细节,实现敏捷开发。
SpringBoot 就是一个工具集。
SpringBoot 特点:
- SpringBoot 项目不需要模板化的配置。
- 在 SpringBoot 中整合第三方框架时,只需要导入相应的 starter 依赖包,就自动整合了。
- SpringBoot 默认只有一个 .properties 的配置文件,不推荐使用 xml,且后期会采用 .java 的文件去编写配置信息,减去了很多配置麻烦问题。
- SpringBoot 工程在部署的时候,采用的是jar包的方式,内部自动依赖 Tomcat 容器,提供了多环境的配置。
二、SpringBoot 快速入门
下面将介绍下怎么快速创建 SpringBoot 项目。
1. 如图所示,依次点击 File -> New -> Project ,选择 SpringBoot Initializr,新建一个 SpringBoot 项目,然后点 Next 。注意,新建过程中必须保持电脑联网,因为创建项目时需要连接到外部地址 https://start.spring.io 。

2. 如图所示是新建项目的相关信息介绍,信息填写好后直接点击 Next 。

3. 手动选择需要依赖的 jar 包,SpringBoot 用默认版本即可,点击 Next ,再次确认项目名,点击 Finish 即完成 SpringBoot 项目新建。注意,第一次创建 SpringBoot 工程,会下载大量依赖,需保证 Maven 已经配置了阿里云私服,否则会很慢甚至会创建失败。

三、SpringBoot 常用注解
1. 启动类 @SpringBootApplication
SpringBoot 的项目一般都会有 XxxApplication 的入口类,入口类中会有 main 方法,这是一个标准的 Java 应用程序的入口方法。
这个入口类都会有 @SpringBootApplication 注解,它让 SpringBoot 自动给程序进行必要的配置,该注解是 SpringBoot 特有的。
这个配置等同于以下几个注解之和:
@SpringBootConfiguration :表示 Application 作为配置文件存在
@EnableAutoConfiguration :表示启用 SpringBoot 内置的自动配置功能
@ComponentScan : 扫描 bean,路径为 Application 类所在 package 以及 package 下的子路径,在 Springboot 中 bean 都放置在该路径以及子路径下。
2. 表现层 @Controller 等。
1 @RestController
2 public class TestController {
3 @RequestMapping("/test")
4 public String test(){
5 return "test!";
6 }
7 }
@RestController 和 @RequestMapping 是 SpringMVC 的注解,不是 SpringBoot 特有的注解。
(1) @RestController
用于处理HTTP请求,@RestController = @Controller + @ResponseBody
(2) @ResponseBody
将 java 对象转为 json 格式的数据。
@ResponseBody 注解的作用是将 controller 的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到 response 对象的 body 区,通常用来返回 JSON 数据或者是 XML 数据。
注意:在使用此注解之后不会再走视图处理器,而是直接将数据写入到输入流中,他的效果等同于通过 response 对象输出指定格式的数据。
@ResponseBody 表示该方法的返回结果直接写入 HTTP response body 中,一般在异步获取数据时使用,如 AJAX。
(3) @RequestMapping
用于配置url映射
@GetMapping 组合注解相当于 @RequestMapping(method = RequestMethod.GET)
@PostMapping 组合注解相当于 @RequestMapping(method = RequestMethod.POST)
(4) @RequestParam
将请求参数绑定到你控制器的方法参数上,是 springmvc 中接收普通参数的注解。
请求中的参数名和处理器中的形参名不一致时用 @RequestParam。
语法:@RequestParam(value = "参数名", required = "true/false", defaultValue = "")
value:参数名
required:是否包含该参数,默认为 true,表示该请求路径中必须包含该参数,如果不包含就报错。
defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值。
1 @GetMapping("/add")
2 public void addUser(@RequestParam("name")String name){
3
4 }
3. 业务层@Service 等。
4. 持久层 @Repository 等。
5. 任意层
(1) @Component
@Component, @Service, @Controller, @Repository 是 Spring 注解,注解后可以被 Spring 框架所扫描并注入到 Spring 容器来进行管理。
@Component 是通用注解,其他三个注解是这个注解的拓展,并且具有了特定的功能。
(2) 读取配置文件
@Value:注入 Spring boot application.properties 配置的属性的值。
(3) 生成 Bean
@Autowired:自动导入依赖的 bean。
@Resource:@Autowired 与 @Resource 都可以用来装配 bean,都可以写在字段上或写在 setter 方法上。
四、SpringBoot常用配置项
SpringBoot项目常用的配置项很全面,在 application.properties 中设置修改即可。格式:配置项 = 配置值,如:spring.thymeleaf.prefix = classpath:/templates/
server
- server.address:指定 server 绑定的地址。
- server.compression.enabled:是否开启压缩,默认为 false。
- server.compression.excluded-user-agents:指定不压缩的 user-agent,多个以逗号分隔,默认值为: text/html,text/xml,text/plain,text/css
- server.compression.mime-types:指定要压缩的MIME type,多个以逗号分隔.
- server.compression.min-response-size:执行压缩的阈值,默认为 2048。
- server.context-parameters.[param name]:设置 servlet context 参数。
- server.context-path:设定应用的 context-path。(如设置context-path=server.context-path,所以访问的路径前缀都要有/springboot。)
- server.display-name:设定应用的展示名称,默认: application。
- server.jsp-servlet.class-name:设定编译 JSP 用的 servlet,默认: org.apache.jasper.servlet.JspServlet)。
- server.jsp-servlet.init-parameters.[param name]:设置 JSP servlet 初始化参数。
- server.jsp-servlet.registered:设定 JSP servlet 是否注册到内嵌的 servlet 容器,默认 true。
- server.port:设定 http 监听端口。
- server.servlet-path:设定 dispatcher servlet 的监听路径,默认为: /。
mvc
-
spring.mvc.async.request-timeout:设定async请求的超时时间,以毫秒为单位,如果没有设置的话,以具体实现的超时时间为准,比如 tomcat 的 servlet3 的话是 10 秒。
-
spring.mvc.date-format:设定日期的格式,比如 dd/MM/yyyy。
-
spring.mvc.favicon.enabled:是否支持 favicon.ico,默认为: true。
-
spring.mvc.ignore-default-model-on-redirect:在重定向时是否忽略默认 model 的内容,默认为 true。
-
spring.mvc.locale:指定使用的 Locale。
-
spring.mvc.message-codes-resolver-format:指定 message codes 的格式化策略(PREFIX_ERROR_CODE,POSTFIX_ERROR_CODE)。
-
spring.mvc.view.prefix:指定 mvc 视图的前缀。
-
spring.mvc.view.suffix:指定 mvc 视图的后缀。
messages
-
spring.messages.basename:指定 message 的 basename,多个以逗号分隔,如果不加包名的话,默认从 classpath 路径开始,默认: messages。
-
spring.messages.cache-seconds:设定加载的资源文件缓存失效时间,-1 的话为永不过期,默认为 -1。
-
spring.messages.encoding:设定 Message bundles 的编码,默认:UTF-8。
mobile
-
spring.mobile.devicedelegatingviewresolver.enable-fallback:是否支持fallback的解决方案,默认false
-
spring.mobile.devicedelegatingviewresolver.enabled:是否开始device view resolver,默认为: false
-
spring.mobile.devicedelegatingviewresolver.mobile-prefix:设定mobile端视图的前缀,默认为:mobile/
-
spring.mobile.devicedelegatingviewresolver.mobile-suffix:设定mobile视图的后缀
-
spring.mobile.devicedelegatingviewresolver.normal-prefix:设定普通设备的视图前缀
-
spring.mobile.devicedelegatingviewresolver.normal-suffix:设定普通设备视图的后缀
-
spring.mobile.devicedelegatingviewresolver.tablet-prefix:设定平板设备视图前缀,默认:tablet/
-
spring.mobile.devicedelegatingviewresolver.tablet-suffix:设定平板设备视图后缀.
-
spring.mobile.sitepreference.enabled:是否启用SitePreferenceHandler,默认为: true
view
-
spring.view.prefix:设定mvc视图的前缀.
-
spring.view.suffix:设定mvc视图的后缀.
resource
-
spring.resources.add-mappings:是否开启默认的资源处理,默认为true
-
spring.resources.cache-period:设定资源的缓存时效,以秒为单位.
-
spring.resources.chain.cache:是否开启缓存,默认为: true
-
spring.resources.chain.enabled:是否开启资源 handling chain,默认为false
-
spring.resources.chain.html-application-cache:是否开启h5应用的cache manifest重写,默认为: false
-
spring.resources.chain.strategy.content.enabled:是否开启内容版本策略,默认为false
-
spring.resources.chain.strategy.content.paths:指定要应用的版本的路径,多个以逗号分隔,默认为:[/**]
-
spring.resources.chain.strategy.fixed.enabled:是否开启固定的版本策略,默认为false
-
spring.resources.chain.strategy.fixed.paths:指定要应用版本策略的路径,多个以逗号分隔
-
spring.resources.chain.strategy.fixed.version:指定版本策略使用的版本号
-
spring.resources.static-locations:指定静态资源路径,默认为classpath:[/META-INF/resources/,/resources/, /static/, /public/]以及context:/
multipart
-
multipart.enabled:是否开启文件上传支持,默认为true
-
multipart.file-size-threshold:设定文件写入磁盘的阈值,单位为MB或KB,默认为0
-
multipart.location:指定文件上传路径.
-
multipart.max-file-size:指定文件大小最大值,默认1MB
-
multipart.max-request-size:指定每次请求的最大值,默认为10MB
freemarker
-
spring.freemarker.allow-request-override:指定HttpServletRequest的属性是否可以覆盖controller的model的同名项
-
spring.freemarker.allow-session-override:指定HttpSession的属性是否可以覆盖controller的model的同名项
-
spring.freemarker.cache:是否开启template caching.
-
spring.freemarker.charset:设定Template的编码.
-
spring.freemarker.check-template-location:是否检查templates路径是否存在.
-
spring.freemarker.content-type:设定Content-Type.
-
spring.freemarker.enabled:是否允许mvc使用freemarker.
-
spring.freemarker.expose-request-attributes:设定所有request的属性在merge到模板的时候,是否要都添加到model中.
-
spring.freemarker.expose-session-attributes:设定所有HttpSession的属性在merge到模板的时候,是否要都添加到model中.
-
spring.freemarker.expose-spring-macro-helpers:设定是否以springMacroRequestContext的形式暴露RequestContext给Spring’s macro library使用
-
spring.freemarker.prefer-file-system-access:是否优先从文件系统加载template,以支持热加载,默认为true
-
spring.freemarker.prefix:设定freemarker模板的前缀.
-
spring.freemarker.request-context-attribute:指定RequestContext属性的名.
-
spring.freemarker.settings:设定FreeMarker keys.
-
spring.freemarker.suffix:设定模板的后缀.
-
spring.freemarker.template-loader-path:设定模板的加载路径,多个以逗号分隔,默认: ["classpath:/templates/"]
-
spring.freemarker.view-names:指定使用模板的视图列表.
velocity
-
spring.velocity.allow-request-override:指定HttpServletRequest的属性是否可以覆盖controller的model的同名项
-
spring.velocity.allow-session-override:指定HttpSession的属性是否可以覆盖controller的model的同名项
-
spring.velocity.cache:是否开启模板缓存
-
spring.velocity.charset:设定模板编码
-
spring.velocity.check-template-location:是否检查模板路径是否存在.
-
spring.velocity.content-type:设定ContentType的值
-
spring.velocity.date-tool-attribute:设定暴露给velocity上下文使用的DateTool的名
-
spring.velocity.enabled:设定是否允许mvc使用velocity
-
spring.velocity.expose-request-attributes:是否在merge模板的时候,将request属性都添加到model中
-
spring.velocity.expose-session-attributes:是否在merge模板的时候,将HttpSession属性都添加到model中
-
spring.velocity.expose-spring-macro-helpers:设定是否以springMacroRequestContext的名来暴露RequestContext给Spring’s macro类库使用
-
spring.velocity.number-tool-attribute:设定暴露给velocity上下文的NumberTool的名
-
spring.velocity.prefer-file-system-access:是否优先从文件系统加载模板以支持热加载,默认为true
-
spring.velocity.prefix:设定velocity模板的前缀.
-
spring.velocity.properties:设置velocity的额外属性.
-
spring.velocity.request-context-attribute:设定RequestContext attribute的名.
-
spring.velocity.resource-loader-path:设定模板路径,默认为: classpath:/templates/
-
spring.velocity.suffix:设定velocity模板的后缀.
-
spring.velocity.toolbox-config-location:设定Velocity Toolbox配置文件的路径,比如 /WEB-INF/toolbox.xml.
-
spring.velocity.view-names:设定需要解析的视图名称.
thymeleaf
-
spring.thymeleaf.cache:是否开启模板缓存,默认true
-
spring.thymeleaf.check-template-location:是否检查模板路径是否存在,默认true
-
spring.thymeleaf.content-type:指定Content-Type,默认为: text/html
-
spring.thymeleaf.enabled:是否允许MVC使用Thymeleaf,默认为: true
-
spring.thymeleaf.encoding:指定模板的编码,默认为: UTF-8
-
spring.thymeleaf.excluded-view-names:指定不使用模板的视图名称,多个以逗号分隔.
-
spring.thymeleaf.mode:指定模板的模式,具体查看StandardTemplateModeHandlers,默认为: HTML5
-
spring.thymeleaf.prefix:指定模板的前缀,默认为:classpath:/templates/
-
spring.thymeleaf.suffix:指定模板的后缀,默认为:.html
-
spring.thymeleaf.template-resolver-order:指定模板的解析顺序,默认为第一个.
-
spring.thymeleaf.view-names:指定使用模板的视图名,多个以逗号分隔.
mustache
-
spring.mustache.cache:是否Enable template caching.
-
spring.mustache.charset:指定Template的编码.
-
spring.mustache.check-template-location:是否检查默认的路径是否存在.
-
spring.mustache.content-type:指定Content-Type.
-
spring.mustache.enabled:是否开启mustcache的模板支持.
-
spring.mustache.prefix:指定模板的前缀,默认: classpath:/templates/
-
spring.mustache.suffix:指定模板的后缀,默认: .html
-
spring.mustache.view-names:指定要使用模板的视图名.
groovy
-
spring.groovy.template.allow-request-override:指定HttpServletRequest的属性是否可以覆盖controller的model的同名项
-
spring.groovy.template.allow-session-override:指定HttpSession的属性是否可以覆盖controller的model的同名项
-
spring.groovy.template.cache:是否开启模板缓存.
-
spring.groovy.template.charset:指定Template编码.
-
spring.groovy.template.check-template-location:是否检查模板的路径是否存在.
-
spring.groovy.template.configuration.auto-escape:是否在渲染模板时自动排查model的变量,默认为: false
-
spring.groovy.template.configuration.auto-indent:是否在渲染模板时自动缩进,默认为false
-
spring.groovy.template.configuration.auto-indent-string:如果自动缩进启用的话,是使用SPACES还是TAB,默认为: SPACES
-
spring.groovy.template.configuration.auto-new-line:渲染模板时是否要输出换行,默认为false
-
spring.groovy.template.configuration.base-template-class:指定template base class.
-
spring.groovy.template.configuration.cache-templates:是否要缓存模板,默认为true
-
spring.groovy.template.configuration.declaration-encoding:在写入declaration header时使用的编码
-
spring.groovy.template.configuration.expand-empty-elements:是使用
<br/>这种形式,还是<br></br>这种展开模式,默认为: false) -
spring.groovy.template.configuration.locale:指定template locale.
-
spring.groovy.template.configuration.new-line-string:当启用自动换行时,换行的输出,默认为系统的line.separator属性的值
-
spring.groovy.template.configuration.resource-loader-path:指定groovy的模板路径,默认为classpath:/templates/
-
spring.groovy.template.configuration.use-double-quotes:指定属性要使用双引号还是单引号,默认为false
-
spring.groovy.template.content-type:指定Content-Type.
-
spring.groovy.template.enabled:是否开启groovy模板的支持.
-
spring.groovy.template.expose-request-attributes:设定所有request的属性在merge到模板的时候,是否要都添加到model中.
-
spring.groovy.template.expose-session-attributes:设定所有request的属性在merge到模板的时候,是否要都添加到model中.
-
spring.groovy.template.expose-spring-macro-helpers:设定是否以springMacroRequestContext的形式暴露RequestContext给Spring’s macro library使用
-
spring.groovy.template.prefix:指定模板的前缀.
-
spring.groovy.template.request-context-attribute:指定RequestContext属性的名.
-
spring.groovy.template.resource-loader-path:指定模板的路径,默认为: classpath:/templates/
-
spring.groovy.template.suffix:指定模板的后缀
-
spring.groovy.template.view-names:指定要使用模板的视图名称.
http
-
spring.hateoas.apply-to-primary-object-mapper:设定是否对object mapper也支持HATEOAS,默认为: true
-
spring.http.converters.preferred-json-mapper:是否优先使用JSON mapper来转换.
-
spring.http.encoding.charset:指定http请求和相应的Charset,默认: UTF-8
-
spring.http.encoding.enabled:是否开启http的编码支持,默认为true
-
spring.http.encoding.force:是否强制对http请求和响应进行编码,默认为true
jackson
-
spring.jackson.date-format:指定日期格式,比如yyyy-MM-dd HH:mm:ss,或者具体的格式化类的全限定名
-
spring.jackson.deserialization:是否开启Jackson的反序列化
-
spring.jackson.generator:是否开启json的generators.
-
spring.jackson.joda-date-time-format:指定Joda date/time的格式,比如yyyy-MM-dd HH:mm:ss). 如果没有配置的话,dateformat会作为backup
-
spring.jackson.locale:指定json使用的Locale.
-
spring.jackson.mapper:是否开启Jackson通用的特性.
-
spring.jackson.parser:是否开启jackson的parser特性.
-
spring.jackson.property-naming-strategy:指定PropertyNamingStrategy (CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)或者指定PropertyNamingStrategy子类的全限定类名.
-
spring.jackson.serialization:是否开启jackson的序列化.
-
spring.jackson.serialization-inclusion:指定序列化时属性的inclusion方式,具体查看JsonInclude.Include枚举.
-
spring.jackson.time-zone:指定日期格式化时区,比如America/Los_Angeles或者GMT+10.
jersey
- spring.jersey.filter.order:指定Jersey filter的order,默认为: 0
- spring.jersey.init:指定传递给Jersey的初始化参数.
- spring.jersey.type:指定Jersey的集成类型,可以是servlet或者filter.
浙公网安备 33010602011771号