spring-boot学习:五、自定义配置

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/guangcaiwudong/article/details/98218678

尽管spring boot通过默认或推荐配置使开发人员更加便捷,但有时候依然需要进行一些自定义的配置,比如端口、编码等,而spring boot也是开放和支持的,只需要在application.properties或application.yml中加入配置即可。具体开放了哪些通用配置或属性可参考官方文档。
https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/htmlsingle/#common-application-properties

1. 自定义banner
每次启动项目时console中都会显示banner信息

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

1)关闭banner信息打印

public static void main(String[] args) {
	SpringApplication app = new SpringApplication(Application.class);
	app.setBannerMode(Banner.Mode.OFF);
	app.run(args);
}

或者在application.properties中配置:

spring.main.banner-mode=off

2) 自定义banner信息

创建banner.txt,参考
https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/htmlsingle/#boot-features-banner

在application.properties中配置banner路径,参考
https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/htmlsingle/#common-application-properties

在这里插入图片描述

2. 自定义端口、编码等

1) 在src/main/resources下增加config目录,创建application.properties文件

# server
server.port=8080
server.tomcat.uri-encoding=UTF-8

#访问项目名称,一般不配置
#server.servlet.context-path=/test 

# http
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

2)SpringApplication对application.properties文件的加载优先级
外置,应用程序相同目录下的config目录
外置,应用程序相同目录下
内置,classpath下config目录
内置,classpath下

在这里插入图片描述
比如,在resource/config和resource目录下同时创建application.properties,SpringApplication会优先加载resource/config下的配置文件。
在这里插入图片描述

3. 第三方jar包的版本号查看
https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/htmlsingle/#appendix-dependency-versions

4. spring-boot包含哪些starters
https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/htmlsingle/#using-boot-starter

猜你喜欢

转载自blog.csdn.net/guangcaiwudong/article/details/98218678