Java Spring Boot 项目中的 application.properties 文件在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38775194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is the application.properties file in a Spring Boot project?
提问by Mira Mira
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!
非常感谢!
采纳答案by Minjun Yu
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
.
If you are not using Maven or any other build tools, put that under your src folder and you should be fine.
如果您使用 Maven 或 Gradle,您可以将文件放在src/main/resources
.
如果您不使用 Maven 或任何其他构建工具,请将其放在 src 文件夹下,您应该没问题。
Then you can just add an entry server.port = xxxx
in the properties file.
然后你可以server.port = xxxx
在属性文件中添加一个条目。
回答by Achille_vanhoutte
回答by alainlompo
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:
- A /config subdirectory of the current directory.
- The current directory
- A classpath /config package
- The classpath root
- 当前目录的 /config 子目录。
- 当前目录
- 一个类路径 /config 包
- 类路径根
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
所以继续创建它
回答by soyphea
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,
server.port = Your PORT(XXXX) by default=8080
server.servlet.context-path=/api (SpringBoot version 2.x.)
server.contextPath-path=/api (SpringBoot version < 2.x.)
application.properties 重要变化,
server.port = Your PORT(XXXX) by default=8080
server.servlet.context-path=/api (SpringBoot version 2.x.)
server.contextPath-path=/api (SpringBoot version < 2) 。X。)
Any way you can use application.ymlin 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