SpringBoot中server.port的默认值为什么是8080以及如何修改端口号

1.如何设置值

首先我们进入ServerProperties中查看其内容:
在这里插入图片描述
发现其类上面有注解
@ConfigurationProperties(
prefix = “server”,
ignoreUnknownFields = true
)
prefix为前缀,前缀名为server所以我们在properties、yaml、yml文件中可以用server.xxx的方式进行属性配置。我们点进pom.xml文件中的spring-boot-starter-parent:
在这里插入图片描述

springboot会去resources路径下加载符合要求的文件,从该文件中查找配置来覆盖默认配置,文件要求如下:
在这里插入图片描述
如果出现多个文件都设置了相同的配置,最后面的文件会覆盖前面文件的配置,比如yaml会覆盖yml,properties会覆盖yaml,也就是说:
properties > yaml > yml

2.默认值为什么是8080

我们导入的依赖都是jar包,默认的配置信息存在 包名字以 *-autoconfigure.jar 形式结尾的jar包中,springboot的默认配置文件的名字为:/META-INF/spring-configuration-metadata.json。
/META-INF/additional-spring-configuration-metadata.json,这个文件中也是默认配置,是/META-INF/spring-configuration-metadata.json 文件中的补充配置信息:
在这里插入图片描述
点进spring-configuration-metadata.json中,查看其内容,发现server.port的默认值在其中已经配置好了,默认值为8080:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/MrYushiwen/article/details/112103559