[英]Where is the application.properties file in a Spring Boot project?
I started a new Spring boot project, I want to change the port number and I read that I have to modify the /resource/application.properties
to do so.我开始了一个新的 Spring boot 项目,我想更改端口号,我读到我必须修改/resource/application.properties
才能这样做。
I cannot locate this file however, did I miss something?但是,我找不到这个文件,我错过了什么吗? Do I need to install a boot starter?我需要安装引导启动程序吗? I don't want to set this using the spring CLI.我不想使用 spring CLI 设置它。
Should I create this file manually?我应该手动创建这个文件吗? If so, I think I'll have to mark this file as the properties file somewhere in the code.如果是这样,我想我必须将此文件标记为代码中某处的属性文件。 Where would that be?那会在哪里?
Thanks a lot!非常感谢!
You will need to add the application.properties
file in your classpath.您需要在类路径中添加application.properties
文件。
If you are using Maven or Gradle, you can just put the file under src/main/resources
.如果您使用 Maven 或 Gradle,您可以将文件放在src/main/resources
。
If you are not using Maven or any other build tools, put that under your src folder and you should be fine.如果您不使用 Maven 或任何其他构建工具,请将其放在 src 文件夹下,您应该没问题。
Then you can just add an entry server.port = xxxx
in the properties file.然后你可以在属性文件中添加一个条目server.port = xxxx
。
You can also create the application.properties file manually.您还可以手动创建 application.properties 文件。
SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment: SpringApplication 将从以下位置的 application.properties 文件加载属性并将它们添加到 Spring Environment:
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).该列表按优先级排序(在列表中较高位置定义的属性覆盖在较低位置定义的属性)。 (From the Spring boot features external configuration doc page ) (来自Spring boot features 外部配置文档页面)
So just go ahead and create it所以继续创建它
In the your first journey in spring boot project I recommend you to start with Spring Starter Try this link here .在您的 Spring Boot 项目的第一次旅程中,我建议您从 Spring Starter 开始尝试这里链接。
It will auto generate the project structure for you like this.application.perperties it will be under /resources.它将自动为您生成项目结构,如 this.application.perperties 它将位于 /resources 下。
application.properties important change , application.properties 的重要变化,
server.port = Your PORT(XXXX) by default=8080 server.port = 默认情况下您的 PORT(XXXX)=8080
server.servlet.context-path=/api (SpringBoot version 2.x.) server.servlet.context-path=/api (SpringBoot 2.x.)
server.contextPath-path=/api (SpringBoot version < 2.x.) server.contextPath-path=/api (SpringBoot 版本 < 2.x.)
Any way you can use application.yml in case you don't want to make redundancy properties setting.如果您不想进行冗余属性设置,则可以使用任何方式使用application.yml 。
Example示例
application.yml应用程序.yml
server:
port: 8080
contextPath: /api
application.properties应用程序属性
server.port = 8080
server.contextPath = /api
Spring Boot will automatically find and load application.properties and application.yaml files from the following locations when your application starts:当您的应用程序启动时,Spring Boot 会自动从以下位置查找并加载 application.properties 和 application.yaml 文件:
The list is ordered by precedence (with values from lower items overriding earlier ones).该列表按优先级排序(较低项目的值覆盖较早的项目)。
More info you can find here https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-files您可以在此处找到更多信息https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-files
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.