📜  Spring Boot属性

📅  最后修改于: 2021-01-11 05:02:11             🧑  作者: Mango

Spring Boot应用程序属性

Spring Boot Framework带有一个内置的机制,用于使用名为application.properties的文件进行应用程序配置。它位于src / main / resources文件夹中,如下图所示。

Spring Boot提供了可以在application.properties文件中配置的各种属性。这些属性具有默认值。我们可以为Spring Boot应用程序设置一个或多个属性。如果需要,Spring Boot还允许我们定义自己的属性。

通过application.properties文件,我们可以在其他环境中运行应用程序。简而言之,我们可以使用application.properties文件来:

  • 配置Spring Boot框架
  • 定义我们的应用程序定制配置属性

application.properties的示例

#configuring application name
spring.application.name = demoApplication
#configuring port
server.port = 8081

在上面的示例中,我们已经配置了应用程序名称port 。端口8081表示应用程序在端口8081上运行。

注意:以#开头的行是注释。

YAML属性文件

Spring Boot提供了另一个配置属性的文件,称为yml文件。 Yaml文件起作用是因为在类路径中存在Snake YAML jar。除了使用application.properties文件,我们还可以使用application.yml文件,但是Yml文件应该存在于类路径中。

application.yml的示例

spring:
application:
name: demoApplication
server:
port: 8081

在上面的示例中,我们已经配置了应用程序名称port 。端口8081表示应用程序在端口8081上运行。

Spring Boot属性类别

Spring Boot属性有十六个类别,如下所示:

  • 核心属性
  • 缓存属性
  • 邮件属性
  • JSON属性
  • 资料属性
  • 交易属性
  • 数据迁移属性
  • 整合属性
  • 网站资源
  • 模板属性
  • 服务器属性
  • 安全性
  • RSocket属性
  • 执行器特性
  • DevTools属性
  • 测试性质

应用程序属性表

下表提供了常见的Spring Boot属性列表:

Property Default Values Description
Debug false It enables debug logs.
spring.application.name It is used to set the application name.
spring.application.admin.enabled false It is used to enable admin features of the application.
spring.config.name application It is used to set config file name.
spring.config.location It is used to config the file name.
server.port 8080 Configures the HTTP server port
server.servlet.context-path It configures the context path of the application.
logging.file.path It configures the location of the log file.
spring.banner.charset UTF-8 Banner file encoding.
spring.banner.location classpath:banner.txt It is used to set banner file location.
logging.file It is used to set log file name. For example, data.log.
spring.application.index It is used to set application index.
spring.application.name It is used to set the application name.
spring.application.admin.enabled false It is used to enable admin features for the application.
spring.config.location It is used to config the file locations.
spring.config.name application It is used to set config the file name.
spring.mail.default-encoding UTF-8 It is used to set default MimeMessage encoding.
spring.mail.host It is used to set SMTP server host. For example, smtp.example.com.
spring.mail.password It is used to set login password of the SMTP server.
spring.mail.port It is used to set SMTP server port.
spring.mail.test-connection false It is used to test that the mail server is available on startup.
spring.mail.username It is used to set login user of the SMTP server.
spring.main.sources It is used to set sources for the application.
server.address It is used to set network address to which the server should bind to.
server.connection-timeout It is used to set time in milliseconds that connectors will wait for another HTTP request before closing the connection.
server.context-path It is used to set context path of the application.
server.port 8080 It is used to set HTTP port.
server.server-header It is used for the Server response header (no header is sent if empty)
server.servlet-path / It is used to set path of the main dispatcher servlet
server.ssl.enabled It is used to enable SSL support.
spring.http.multipart.enabled True It is used to enable support of multi-part uploads.
spring.servlet.multipart.max-file-size 1MB It is used to set max file size.
spring.mvc.async.request-timeout It is used to set time in milliseconds.
spring.mvc.date-format It is used to set date format. For example, dd/MM/yyyy.
spring.mvc.locale It is used to set locale for the application.
spring.social.facebook.app-id It is used to set application’s Facebook App ID.
spring.social.linkedin.app-id It is used to set application’s LinkedIn App ID.
spring.social.twitter.app-id It is used to set application’s Twitter App ID.
security.basic.authorize-mode role It is used to set security authorize mode to apply.
security.basic.enabled true It is used to enable basic authentication.
Spring.test.database.replace any Type of existing DataSource to replace.
Spring.test.mockmvc.print default MVC Print option
spring.freemaker.content-type text/html Content Type value
server.server-header Value to use for the server response header.
spring.security.filter.dispatcher-type async, error, request Security filter chain dispatcher types.
spring.security.filter.order -100 Security filter chain order.
spring.security.oauth2.client.registration.* OAuth client registrations.
spring.security.oauth2.client.provider.* OAuth provider details.