Spring Boot application.properties configuration file content __spring

Source: Internet
Author: User
Tags mongodb redis

SPRING-BOOT Official Development Guidance Document

When the Spring-boot project is created by default, an empty application.properties configuration file is generated in the resources directory, which is loaded springboot the configuration file at startup.

Application.properties (or APPLICATION.YML) contains information such as system properties, environment variables, and command parameters.

The following is a brief introduction to some of the common configurations in application.properties within the Spring-boot project, with more reference to official documentation.

These parameter configurations are not necessarily written in application.properties and can be configured in application.properties to specify a custom profile name and location: (but no matter how it is configured, Spring-boot will read the load APPLICATION.P roperties file)

Spring.config.name= Custom profile Name

spring.config.location= configuration file location (can be classpath or valid URL)

You can also specify to read a profile by setting the @propertysource annotation on a custom class
1, command line parameters

SERVER.ADDRESS=XXX.XXX.XX.XXX//server binding IP address, multiple NIC can be specified

Server.port=xxx

You can specify the ports that the Springboot embedded container launches, and when using the Tomcat container on port 8080, right-click Run-java application/springboot ..., you can support different containers when introducing different dependencies. When server.port=0, the automatic sweep surface is used to obtain an available port.

Secure access configuration for SSL

server.port=8443
server.ssl.key-store=classpath:keystore.jks
server.ssl.key-store-password=secret
Server.ssl.key-password=another-secret

Currently, Spring-boot does not support HTTP and HTTPS enabled at the same time, only one of them is supported, and other forms of implementation can be used if they need to be used simultaneously.

This section corresponds to the Org.springframework.boot.autoconfigure.webServerProperties class.


In addition, there are some not very commonly used, such as:

Server.http2.enable=true/false

This property can support the HTTP2 protocol type, currently supports only Tomcat and Undertow containers and requires jdk1.8+.


Guanven on the embedded Tomcat configuration parameters also have a lot, can also be encoded to achieve a lot of tomcat functions, not to say more, content, need to look at the official website.


2. System Properties/Variables 2.1 Development/test/Production environment configuration

Spring.profiles.active=xxxx

The system variable can indicate the configuration file to use, typically used for multiple-environment configuration separation, such as production environment (production), development environment (development), test environment (testing), etc., can be customized, If the development environment profile is application-dev.properties, Spring.profiles.active=dev, the application-dev.properties configuration file is loaded at startup.


2.2 about Banner

When the project starts, it prints a whole bunch of symbols, and it's been a long time since I've seen a few letters like spring in print.

Springboot is called banner configuration.

Banner.location=xxx.txt//Can customize the location of the output information

Banner.charset=utf-8//Specify encoding format

Spring.main.banner-mode=console/off//banner graphics Open or Print mode


2.3 MySQL Data source configuration (introduced SPRING-BOOT-STARTER-JDBC automatic integration)

Annotations use JdbcTemplate or integrated MyBatis (introducing Maven:mybatis-spring-boot-starter)

Spring.datasource.driver-class-name=com.mysql.jdbc.driver
Spring.datasource.url=jdbc:mysql://localhost:3306/company?allowmultiqueries=true
Spring.datasource.username=root
Spring.datasource.password=xxxx

This section is configured as a spring data source configuration, and database operations using JdbcTemplate (using jdbctempate automatically prompts for dependencies such as SPRING-BOOT-STARTER-JDBC). When you are working with unit tests, running multiple data source conflicts that may occur at the same time, you can generate a unique data source name by using the following properties

Spring.datasource.generate-unique-name=true

The Spring.datasource.* property set configuration corresponds to the Org.springframework.boot.autoconfigure.jdbcDataSourceProperties class.


2.4 MongoDB Data Source configuration (introduced Spring-boot-starter-data-mongodb automatic integration)

spring.data.mongodb.* the corresponding attribute class is Mongoproperties.java

Common basic Properties:

SPRING.DATA.MONGODB.HOST=XXX//default localhost

SPRING.DATA.MONGODB.PORT=XXX//Default 27017

SPRING.DATA.MONGODB.DATABASE=XX//Database

SPRING.DATA.MONGODB.USERNAME=XX//Database user name

SPRING.DATA.MONGODB.PASSWORD=XX//Database user password

The above parameters can also be replaced with one: (this parameter cannot be used concurrently with this parameter)

Spring.data.mongodb.uri=mongodb://localhost:27017/test

If you have a password:

Mongodb://username:password@localhost:27017/dbname


2.5 Redis configuration (introduced Spring-boot-starter-data-redis automatic integration)

#spring. redis.url=
#上面url形式等同下面
#host和port默认值取值localhost, 6379
Spring.redis.host=localhost
spring.redis.port=6379

#链接池配置, below is the default value

Spring.pool.max-idle=8

Spring.pool.min-idel=0

Spring.pool.max-wait=-1//-1 Disables this property and waits for the unit milliseconds

Spring.pool.max-active=8

#密码, no password required
#spring. redis.password=
#spring. Redis.ssl=true/false

Corresponding class Org.springframework.boot.autoconfigure.data.redis.RedisProperties.java class


2.6 MyBatis configuration (introduced Mybatis-spring-boot-starter automatic integration)

If the XML mapping file configuration is not applicable, the following is not required, and using annotations directly can implement

#本例子中放在resources/mybatis/mybatis-config.xml.

Mybatis.config-location=classpath:mybatis/mybatis-config.xml

Mybatis.mapper-locaitons=classpath:mybatis/mappings/*.xml


Configuring in #下面的配置可以在mybatis-config.xml

#mybatis. configuration.*=xxx
#别名实体包, separated by multiple commas
#mybatis. Type-aliases-package=com.tom.bean
#类型转换器包, separated by multiple commas
#mybatis. type-handlers-package=com.tom.mybatis.handlers
#执行类型
#mybatis. Executor-type=simple/reuse/batch


2.7 Cache Configuration (introduction of Spring-boot-starter-cache automatic integration)

For caching automatic integration, Spring-boot also provides a number of scenarios, here is the default Spring-cache, Ehcache, Redis-cache related property configuration

Spring.cache.type=cache/ehcache/redis ..... (Reference CacheType Class)//used to indicate the type of cache to use

Ehcache Special:

Spring.cache.ehcache.config=classpath:cache/ehcache.xml//Used to configure the location of the configuration file when using Ehcache


When you use the default cache (the default cache is a CONCURRENTMAP implementation of caching, provided that no other cache dependencies such as Spring-boot-starter-data-redis are introduced), or when using Redis, you can use the following parameter configuration

SPRING.CACHE.CACHE-NAMES=XXX,XXX,XXX//config cache name


Redis Special:

spring.cache.redis.time-to-live=600000//cache active time, unit millisecond


Spring.cache.* corresponding class is: Org.springframework.boot.autoconfigure.cache.cacheProperties.java

You can use the spring annotations in your project to complete the above settings, but use @enablecaching to enable annotations, as detailed in the following sections for cache usage.


2.8 File Upload configuration

#默认true

#spring. http.mutipart.enabled=true

#上传中转文件位置,
#spring. http.multipart.location=
#最大上传文件大小, default 1MB
Spring.http.multipart.max-file-size=5mb
#最大请求大小, default 10MB
spring.http.multipart.max-request-size=20mb

Corresponding class: Org.springframework.boot.autoconfigure.web.MultipartProperties


(Note: In the actual class, there is a hump-represented variable, a configuration property word between-split)


3, springboot official website basic attribute collection # ===================================================================   # COMMON  spring boot properties   #   # this sample file is  provided as a guideline. do not copy it in its   #  entirety to your own application.                 ^^ ^   # ==================================================== ===============         # ----------------------------------------   # core properties   # ----------------------------------------      # banner   banner.charset=utf-8 # banner file encoding.   banner.location=classpath:banner.txt # banner file location.   Banner.image.location=classpath:banner.gif # banner image file location  (jpg/png can also be  Used) .   banner.image.width= # width of the banner image in  chars  (default 76)    Banner.image.height= # height of the banner  image in chars  (default based on image height)    banner.image.margin= # left hand image margin in chars  (default 2 )    banner.image.invert= # if images should be inverted for  dark terminal themes  (default false)      

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.