SpringBoot常用配置说明

本文最后更新于:3 年前

354bc4457b33051bac610341c3f44549.jpg

Speingboot配置的关键点的整理。

修改默认端口

1
server.port=9999
  • 修改随机端口
1
2
#随机端口
server.port=${random.int[8080,8999]}

随机端口的好处就是:
1:如果在同一台服务器上多个服务如果用同一个端口会造成端口冲突
2:在微服务项目和开发中,开发人员是不会去记住ip和端口的。我们一般在真实的开发环境下,设置一个随机端口,就不用去管理端口了。也不会造成端口的冲突。

优先级

yml和properties文件的不同:

  • 如果properties和yml都配置:properties的优先级要高于yml配置
  • 如果两者不同的地方,取并集,相同的部分properties覆盖yml中的配置

环境隔离

如何做到环境隔离呢?

新建对应环境隔离的配置文件,命名规则是:applicaiton-xxxx.yml 比如

application.yml 主配置文件
application-dev.yml 环境隔离文件—dev 开发环境
application-prod.yml 环境隔离文件—prod 生产环境

激活环境,在主配置文件中application.yml 如下

1
2
3
4
5
# 选择环境激活
spring:
profiles:
# active: dev
active: prod

好处:

因为在开发中,开发环境和生产环境配置(MYSQL、Redis等IP和端口)不同,通过环境隔离的方式就很好的解决这个问题。·

运行时JAR包指定配置文件

1
java -jar ***.jar --spring.profiles.active=prod

自定义属性注入

前提:这个类必是spring管理的类

@Value注入

@Value(“${key}”) 存在的问题:不具有面向对象的特征。

配置

在配置文件中添加:

1
2
3
4
5
6
7
#自定义属性
ksd:
weixin:
appid: xxxxxxxxxxxx
mcid: xxxxxxx
callbackurl: https://xxxx.xxxxxx.xxxx/callback
apisecret: xxxxxxxxxxxxxxxxx

注解:@Value(“${对应配置文件的key}”)

1
2
@Value("${ksd.weixin.appid}")
private String appid;

@ConfigurationProperties注入

具有面向对象特种的属性注入的方式。

配置

1
2
3
4
5
6
7
8
9
10
11
@ConfigurationProperties(prefix ="ksd.weixin")
public class WeixinPayProperties {
// appid
private String appid;
// 商户号
private String mcid;
// 回调地址
private String callbackurl;
// api私钥
private String apisecret;
}

自定义属性配置类自动提示

问题

或者出现红色警告:

1624197064219

springboot确实可以去帮你完成属性和配置文件中的属性自动注入的问题,但是不能在配置文件中自动提示

解决

参考官网:https://docs.spring.io/spring-boot/docs/2.4.7/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor

1.在项目的==pom.xml==中配置如下:

1
2
3
4
5
6
7
<!--把项目中的springboot自定义属性配置类生成一个元素数据文件,这个文件可以生成以后
在未来的配置文件中,我们就达到和官方一致效果,可以自动提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

2.重新编译工程

执行 mvn clean compile

3.在重新打开application.yml 就可以看见自动提示的效果。

starter

作用

简化SpringBoot构建配置。

打包好与常用模块相关的所有 JAR包 ,并完成自动配置,然后组装成Starter。

在代码开发时,漫画框架的配置,只用关心业务逻辑即可。

starter是一组方便的依赖项描述符,您可以将其包含在您的应用程序中。您可以一站式地获得所需的所有Spring和相关技术,而不必遍历示例代码和复制-粘贴加载依赖关系描述符。

starter的命名规范:

  • 官方的是spring-boot-starter-xxxx
  • 第三方的名的是 xxxx-boot-starter

常用的Starter

Stater 说明
spring-boot-starter-web 用于构建Web。包含RESTful风格框架,SpringMVC和默认的嵌入式容器Tomcat
spring-boot-starter-test 用于测试
spring-boot-starter-jdbc 传统的JDBC。轻量级应用可以使用,学习成本低,但最好使用JPA或Mybatis
spring-boot-starter-thymeleaf 支持thymeleaf模版
spring-boot-starter-mail 支持Java Mail、Spring Email发送邮件
spring-boot-starter-integration Spring 框架创建的一个API,面向企业应用集成(EAI)
spring-boot-starter-data-redis 通过Spring Data Redis、Redis Client使用Redis
spring-boot-starter-websocket 相对于非持久的协议HTTP,websocket是一个持久化的协议
spring-boot-starter-security 使用Spring Security进行身份验证和授权

第三方的和官方的差异是:

答案:就是版本的管理

官方的版本全部根据parent自动匹配

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

第三方(自定义的)必须一定要指定版本

1
2
3
4
5
 <dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!