Spring Boot v2.0.5 基础教程1

简介本文档主要介绍spring boot V2.0.5 版本的配置文件(configuration)使用建议Eclipse安装下spring tools 插件,该插件并非必须,只是为了方便开发。Eclipse安装参考文档:https://blog.csdn.net/zcl_love_wx/article/details/690579001.配置文件介绍默认配置文件:applicatio...
摘要由CSDN通过智能技术生成
简介

本文档主要介绍spring boot V2.0.5 版本的使用,主要包含五个内容:

  1. 配置文件
  2. 日志
  3. MVC
  4. WEB安全性(Security),包含权限控制,CSRF。
  5. 访问数据库

本文档代码下载地址: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&#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑾析编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值