Spring Boot基础
来源:互联网 发布:人工智能产业峰会 编辑:程序博客网 时间:2024/06/06 00:36
Spring Boot中的application.properties主要用来配置数据库连接、日志相关配置等。除了这些配置内容之外,本文将具体介绍一些在application.properties配置中的其他特性和使用方法。
1.自定义属性与加载
我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义:
demo.url=baidu.com demo.key=what is spring boot #甚至可以采用组合的方式,讲上面两个字符内容进行了拼接 demo.completeurl=${demo.url}?k=${demo.key}
然后通过@Value(“${属性名}”)注解来加载对应的配置属性,具体如下:
@Componentpublic class BlogProperties { @Value("${demo.url}") private String url; @Value("${demo.key}") private String key; @Value("${demo.completeurl}") private String completeurl; // 省略getter和setter}
2.产生随机数
在一些情况下,有些参数我们需要希望它不是一个固定的值,比如密钥、服务端口等。Spring Boot的属性配置文件中可以通过${random}来产生int值、long值或者string字符串,来支持属性的随机值。
# 随机字符串com.value=${random.value}# 随机intcom.number=${random.int}# 随机longcom.bignumber=${random.long}# 10以内的随机数com.test1=${random.int(10)}# 10-20的随机数com.test2=${random.int[10,20]}
3.多环境配置
我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误的事。
在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:
- application-dev.properties:开发环境
- application-test.properties:测试环境
- application-prod.properties:生产环境
需要引入哪一个配置,则只需更改一个地方
#test则匹配application-test.properties文件,其他同理spring.profiles.active=test
最后附上一些spring boot 常见的配置信息
#程序启动后端口号server.port=8889#多环境配置spring.profiles.active=test#mysql连接配置spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver#整体编码设置spring.http.encoding.force=truespring.http.encoding.charset=UTF-8spring.http.encoding.enabled=trueserver.tomcat.uri-encoding=UTF-8#thymeleaf模板配置#配置返回路径spring.thymeleaf.prefix=classpath:/templates/ #匹配的后缀spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=UTF-8;charset=UTF-8spring.thymeleaf.content-type=text/html#是否缓存到浏览其,测试环境下建议falsespring.thymeleaf.cache=false # redis整体设置# Redis数据库索引(默认为0)spring.redis.database=0 # Redis服务器地址spring.redis.host=192.168.0.58# Redis服务器连接端口spring.redis.port=6379 # Redis服务器连接密码(默认为空)spring.redis.password= # 连接池最大连接数(使用负值表示没有限制)spring.redis.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制)spring.redis.pool.max-wait=-1 # 连接池中的最大空闲连接spring.redis.pool.max-idle=8 # 连接池中的最小空闲连接spring.redis.pool.min-idle=0 # 连接超时时间(毫秒)spring.redis.timeout=0
阅读全文
0 0
- Spring boot 基础
- spring-boot 基础入门
- Spring Boot 基础
- Spring Boot基础
- Spring Boot基础
- Spring Boot基础
- Spring Boot基础
- spring boot 基础(一)
- Spring Boot 基础
- Spring Boot基础之“什么是Spring Boot”
- spring boot 基础日志配置
- Spring Boot 基础学习文章
- Spring Boot数据库基础操作
- spring boot 基础日志配置
- spring boot项目基础搭建
- Spring boot/cloud 基础文档
- Spring Boot实战之Spring基础配置
- spring boot零基础学习 spring boot简介(一)
- java httpclient 模拟登陆京东
- 大数据工程师技能图谱
- Eclipse运行main类抛出异常:Unsupported major.minor version 52.0
- Android开机广播
- iOS开发~制作同时支持armv7,armv7s,arm64,i386,x86_64的静态库.a
- Spring Boot基础
- I/O多路复用之select
- 几维安全:为客户提供全方位的移动应用安全服务
- HEVC中编码每个CTU的码流跟踪
- 叹,时光飞逝
- [重要] -- swag.ger生成 PHP restful API 接口文档
- 利用xshell及rz,sz实现拖曳上传文件到Linux服务器
- 一分钟秒懂公有云、私有云、混合云......
- 微信小程序 textarea 不显示文本解决办法