简体   繁体   English

Spring Boot 项目中的 application.properties 文件在哪里?

[英]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:

  • A /config subdirectory of the current directory.当前目录的 /config 子目录。
  • The current directory当前目录
  • A classpath /config package一个类路径 /config 包
  • The classpath root类路径根

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所以继续创建它

You can create it manually but the default location of application.properties is here您可以手动创建它,但 application.properties 的默认位置在这里

在此处输入图片说明

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 文件:

  1. The classpath root类路径根
  2. The classpath /config package类路径 /config 包
  3. The current directory当前目录
  4. The /config subdirectory in the current directory当前目录下的/config子目录
  5. Immediate child directories of the /config subdirectory /config 子目录的直接子目录

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.

相关问题 Spring boot application.properties 文件读取 - Spring boot application.properties file reading Spring 引导无法识别 application.properties 文件 - Spring Boot not recognizing application.properties file 如何排除默认的application.properties在Spring启动项目的Maven中使用配置文件添加自定义属性文件? - How to exclude default application.properties add custom properties file using profiles in maven for spring boot project? spring boot jar中的application.properties文件在哪里? - Where are the application.properties files in a spring boot jar? Spring 引导项目未绑定来自 application.properties 的 @Value - Spring Boot project not binding @Value from application.properties 使用Spring Boot时在eclipse中的哪里添加application.properties文件 - Where to add application.properties file in ecclipse when using spring boot Spring 引导应用程序不使用 Application.Properties 文件 - Spring Boot Application not using Application.Properties file 如何从 spring boot 项目的 application.properties 文件中获取属性值? - How to get property value from application.properties file on spring boot project? 为什么自动编译不适用于 spring 引导项目的 vs 代码中的 application.properties 文件? - why autocomplition is not working for application.properties file in vs code for spring boot project? Spring Boot应用程序不拉入外部application.properties文件 - Spring boot application not pulling external application.properties file in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM