简介
本文档主要介绍spring boot V2.0.5 版本的使用,主要包含五个内容:
- 配置文件
- 日志
- MVC
- WEB安全性(Security),包含权限控制,CSRF。
- 访问数据库
本文档代码下载地址:https://github.com/yushao4170/springboot2.0.5-demo
建议Eclipse安装下spring tools 插件,该插件并非必须,只是为了方便开发。Eclipse安装参考文档:https://blog.csdn.net/zcl_love_wx/article/details/69057900
1.配置文件介绍
默认配置文件:application.properties
application.properties是spring boot的默认配置,如果不喜欢这个名字可以在启动的时候替换该文件。多个文件用,分隔。
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/
override.properties
添加配置文件
使用注解@PropertySource
多环境配置文件-profiles
在Spring Boot中, 多环境配置的文件名需要满足application-{profile}.
properties的格式, 其中{profile}对应你的环境标识, 如下所示。
- application-dev.properties: 开发环境。
- application-test.properties: 测试环境。
- application-prod.properties: 生产环境。
至于具体哪个配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置, 其值对应配置文件中的{profile}值。如spring.profiles.active=test就会加载application-test.properties配置文件内容。
2.日志
# 日志等级分为:ERROR, WARN, INFO, DEBUG, TRACE.
debug=true
# 日志持久化文件,logging.file属性:写入指定的日志文件。名称可以是精确位置或相对于当前目录。
logging.file=D:\\log.log
# 日志持久化文件,logging.path属性:写入spring.log指定的目录。名称可以是精确位置或相对于当前目录。
logging.path=D:\\
3.Spring Web MVC框架
3.1.HttpMessageConverters - 内容转换器
package com.example.demo;
import java.text.SimpleDateFormat;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
/**
* @author guoyu.huang
* @version 1.0.0
*/
@Configuration
public class ApplicationConfiguration {
@Bean
public HttpMessageConverters customConverters() {
// 将日期按"yyyy-MM-dd HH:mm&#