什么是Spring Boot?
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
自己的理解就是spring boot其实不是什么新的框架,它只是默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架,需要哪个框架的时候,只需要新建项目的时候勾选相应的框架核心,然后不需要引入额外的Jar包,因为spring boot帮我们做了这件事。
spring boot的特点
- 创建独立的Spring应用程序
- 嵌入的Tomcat,无需部署WAR文件,能直接运行程序
- 简化Maven配置,不需要在pom.xml文件引入过多的jar包
- 自动配置Spring,不需要新建spring的核心配置文件
- 提供生产就绪型功能,如指标,健康检查和外部配置
- 绝对没有代码生成并且对XML也没有配置要求
spring boot的优点
使用spring boot搭建项目,其突出优点就是简单、快速、方便!
平时搭建一个spring web项目的时候可能需要以下步骤:
1.配置web.xml,加载spring核心配置文件和spring mvc的配置文件
2.配置数据库连接、配置spring事务
3.配置加载配置文件的读取,开启注解
4.配置日志文件
…
配置完成之后部署tomcat 调试
…
等一系列操作,可能一不小心就漏了哪个步骤,又得重新检查一遍。。。
而spring boot可以帮你解决传统的spring web项目构建的繁琐步骤,一步到位。
说了这么多,接下来一起来学习怎样使用吧。(它可以应用在快速搭建web项目和为服务)
搭建一个简单的Spring Boot
- 新建一个全新的project
springboot2.0以上的版本,jdkz最好选择jdk1.8以上
因为需要访问其官网下载初始化的程序,镜像在国外,所以初始化第一个project的时候很慢,以后在此project新建Model就不用加载如此慢。
2.看一下新建的spring boot的project
3.测试能否不配置繁琐的spring的各种配置文件,能否访问成功
1)
在此目录下新建controller包,包下新建TestController.java
package com.springboot.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 小思
* @PackageName:com.springboot.springboot.controller
* @ClassName: TestController
* @Description:测试spring boot访问控制器
* @date 2018/11/18 20:07
*/
@Controller
public class TestController {
@RequestMapping("/hello")
public String sayHello(){
System.out.println("hello spring boot");
return "index";
}
}
2)启动服务器,运行项目
环境搭建成功!
下面是博主搜集的常用的application.properties配置信息
搜集地址:https://blog.csdn.net/je_ge/article/details/54783184
SPRING CONFIG (ConfigFileApplicationListener)
spring.config.name
配置文件名称,默认为application
spring.config.location
配置文件存放位置,默认为classpath目录下
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路径是否存在.