springboot系列(四)springboot 配置
在springboot出现之前,spring项目会存在多个配置文件,如web.xml,配置spring的多个spring-xxx.xml,xxx代表配置spring的某一功能,如aplication-datasource.xml、
appliction-mvc.xml。应用自身也需要多个配置文件,还需要编写代码去读取这些配置文件的参数。现在springboot简化了spring配置的管理和读取,只需要一个application.properties并提供了多种读取配置文件的方式。注(SpringBoot提供了properties和yml两种配置方式)
1、springboot的支持两种配置方式:(两中配置方式的区别请自行百度)
- application.properties
 - application.yml
 
2、springboot常用配置:
A、yml格式
#数据源配置
spring:
# 环境 dev|test|pro
profiles:
active: dev
#服务器配置
server:
tomcat:
uri-encoding: UTF-8
max-threads: 1000
min-spare-threads: 30
port: 8080
#mybatis配置
mybatis:
mapper-locations: classpath*:mybatis/*Mapper.xml
type-aliases-package: com.demo.entity
# jsp
# mvc:
# view:
# prefix: /WEB-INF/ # 页面默认前缀目录
# suffix: .jsp # 响应页面默认后缀 #热部署
devtools:
restart:
enabled: true #热部署生效
#设置重启的目录
additional-paths: src/main/java
exclude: WEB-INF/** #classpath目录下的WEB-INF文件夹内容修改不重启
B、properties方式
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
指定使用模板的视图名,多个以逗号分隔. mustcache
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 json
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.
3、读取应用配置
springboot 提供了三种方式读取配置文件
- Enviroment类,可以通过key-value方式读取到application.properties
 
@Configuration
public class MyProperty {
//注入environment 通过environment获取
@Autowired
private Environment environment;
public void init(){
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, "jdbc.");
String url = environment.getProperty("url");
}
}
- @value
 
public class MyProperty  {  
    // 通过注解value进行填充值
    @Value("${jdbc.url}")
    private String url;  
}  
- @ConfigurationProperties 读取application属性配置文件中的属性。
 
@Getter
@Setter
@Configuration //用java代码的形式申明注册bean
@PropertySource(value = {"classpath:application.properties"},encoding="utf-8")//@PropertySource注解用于指定目录,指定编码读取properties文件
@ConfigurationProperties(prefix = "spring.datasource.sakila") //把同类的配置信息自动封装成实体类
public class DbConfigInfo {
private String url;
private String username;
private String password;
private String driverClassName;
private String initialSize;
private String maxIdle;
private String minIdle;
}
4、配置文件优先级
config/application-default.properties > config/application-default.yml >
application-default.properties > application-default.yml >
config/application.properties > config/application.yml >
application.properties > application.yml
链接:http://www.imooc.com/article/23670
springboot系列(四)springboot 配置的更多相关文章
- springboot系列四、配置模板引擎、配置热部署
		
一.配置模板引擎 在之前所见到的信息显示发现都是以 Rest 风格进行显示,但是很明显在实际的开发之中,所有数据的显示最终都应该交由页面完成,但是这个页面并不是*.jsp 页面,而是普通的*.html ...
 - SpringBoot系列之profles配置多环境(篇二)
		
SpringBoot系列之profles配置多环境(篇二) 继续上篇博客SpringBoot系列之profles配置多环境(篇一)之后,继续写一篇博客进行补充 写Spring项目时,在测试环境是一套数 ...
 - SpringBoot系列之外部配置用法简介
		
SpringBoot系列之外部配置用法简介 引用Springboot官方文档的说法,官方文档总共列举了如下用法: 1.Devtools global settings properties on yo ...
 - SpringBoot系列之YAML配置用法
		
1.全局配置 SpringBoot的全局配置文件有两种: application.properties application.yml 配置文件的作用:修改SpringBoot自动配置的默认值,主要是 ...
 - SpringBoot 系列教程自动配置选择生效
		
191214-SpringBoot 系列教程自动配置选择生效 写了这么久的 Spring 系列博文,发现了一个问题,之前所有的文章都是围绕的让一个东西生效:那么有没有反其道而行之的呢? 我们知道可以通 ...
 - SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)
		
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...
 - SpringBoot系列之profles配置多环境(篇一)
		
SpringBoot profles配置多环境 23/100 发布文章 u014427391 软件环境简介 这里介绍一下SpringBoot提供的profiles属性加上maven配置一下多环境,在实 ...
 - springboot系列四:springboot整合mybatis jsp
		
一.用IDEA 创建maven项目 项目目录结构 1.添加pom jar依赖 <?xml version="1.0" encoding="UTF-8"?& ...
 - 七、springboot(四)配置redis
		
1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
 - Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
		
前言 由于在开发My Blog项目时使用了大量的技术整合,针对于部分框架的使用和整合的流程没有做详细的介绍和记录,导致有些朋友用起来有些吃力,因此打算在接下来的时间里做一些基础整合的介绍,当然,可能也 ...
 
随机推荐
- 微信小程序--TabBar不出现
			
今天打算开始实战一个微信小程序项目,一开始就踩坑了,正确设置了TabBar,但是TabBar就是不能显示出来,后面才找到原因,这里记录下: app.json正确代码: { "pages&qu ...
 - Java测试当前应用所占用的内存示例
			
package test; import java.util.HashMap; import java.util.Map; public class TestMemory { public stati ...
 - 【Leetcode_easy】1103. Distribute Candies to People
			
problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...
 - CSS基础(html+css基础)
			
css: CSS全称为“层叠样式表 (Cascading Style Sheets)”,它主要是用于定义HTML内容在浏览器内的显示样式,如文字大小.颜色.字体加粗等. 1.CSS代码语法: css ...
 - Xena L23网络测试仪Valkyrie使用技巧100例:修改设备管理IP,设备关机 (编号01)
			
Xena Valkyrie产品提供100M~400Gbps全速率接口速率支持 产品链接 https://xenanetworks.com/valkyrie/ 需求# 1.多个用户如何共享使用一个机箱? ...
 - QtCreator中打开.ui文件时卡死崩溃的解决方法
			
问题 QtCreator中打开一个项目,在编辑器中打开普通的.cpp或.h等文件正常,构建正常,运行正常,但是打开ui文件(QtCreator自动调用designer)时会卡死然后崩溃退出. 解决方法 ...
 - Linux下载安装mysql5.7教程
			
首先下载mysql 的安装包,可以去官网下载,网址:https://dev.mysql.com/downloads/mysql/ 然后下滑,在这个地方选择你想要安装的版本: 这里我选择的5.7.点击进 ...
 - Navicat通过跳板机连接MySQL(2层跳转)
			
情景描述,公司开发数据库部署在内网,而且这个开发数据库有连接需要有IP验证,就是只能在内网的某个IP才能连接,所以每次连接都会先连接外网能访问的跳板机,在从跳板机上ssh到内网上的A机器,在从A机 ...
 - Office常用快捷键大全,包含 Word、Excel、PowerPoint
			
功能 快捷键 查找文字.格式和特殊项 Ctrl+G 使字符变为粗体 Ctrl+B 为字符添加下划线 Ctrl+U 删除段落格式 Ctrl+Q 复制所选文本或对象 Ctrl+C 剪切所选文本或对象 Ct ...
 - nginx如何调用PHP(nginx+php运行原理)
			
采用nginx+php作为webserver的架构模式,在现如今运用相当广泛.然而第一步需要实现的是如何让nginx正确的调用php.由于nginx调用php并不是如同调用一个静态文件那么直接简单,是 ...