Springboot Series: Java application and springboot parameter files

Source: Internet
Author: User


===========================
Several forms of parameter transfer to Java programs:
===========================
1. Use the OS environment variable.
This is not recommended.
2. Using JVM variables, the most common is the-D parameter. The-xx and-X parameters are used to set memory and GC parameters, and different JVM parameter settings may vary.
The-D and-X parameters are immediately behind Java.
Java-dfile.encoding=utf-8-jar Abc.jar
Java-dkey=value1-jar Abc.jar
In a Java program, you can obtain the-D parameter in the following way:
String value = System.getproperty ("Key", "DefaultValue");
3. By setting the java_opts or java_tool_options of the two operating system environment variables to pass the parameter.
We can also set the JAVA_OPTS OS environment variable in such a way that the new boot JVM will automatically read the two OS environment variables.
Java_opts= "-djava.library.path=/export/home/actuate7/bin-xms256m-xmx512m"
Java-jar Abc.jar
4. Use the application's own parameters.
Java-jar Abc.jar--argument2=value2
Java-jar Abc.jar--server.port=9000

Special Note:
1. [Recommended] The parameters of Springboot can be set by------parameter, for example:
Java-jar Abc.jar--server.port=9000
Java-jar Abc.jar--spring.profiles.active=prod
2. You can set the Springboot parameter by using the-D parameter or the Java_opts/java_tool_options operation environment variable, for example:
Java-dserver.port=9000-jar Abc.jar
--spring.profiles.active=prod


===========================
Several parameters related to the Springboot parameter file
===========================

-------------------
Spring.config.name parameters
-------------------
The Spring.config.name parameter specifies the parameter file name, which cannot be added to the path and file name extension.
The default value of the Spring.config.name parameter is application,
$ Java-jar Myproject.jar--spring.config.name=myproject
$ Java-jar Myproject.jar--spring.config.location=classpath:/default.properties,classpath:/override.properties


-------------------
Spring.config.location parameters
-------------------
The spring.config.location parameter can either be passed into the directory or be passed into a specific file name, or even multiple directories or files can be passed in (separated by commas).
If the parameter is a directory, the directory should end with/, and spring will look for the spring.config.name corresponding file in the specified directory.
$ Java-jar Myproject.jar--spring.config.location=your/config/dir/
$ Java-jar Myproject.jar--spring.config.location=classpath:job1.properties,classpath:job2.properties

Spring.config.location parameter if default, Springboot will search for the parameter file from the following four paths, the order of the search is:
Config subdirectory of the directory where the file:./config/Soso jar program is located
File:./to search for a configuration file in the same directory as the JAR program
classpath:/config/search for configuration files in the Classes/config directory inside the jar package
classpath:/ Search for configuration files under the classes directory inside the jar package
It should be stated that once the Spring.config.location parameter is specified, Springboot will no longer search for the above 4 directories.

-------------------
Spring.config.additional-location parameters
-------------------
As mentioned above, once the Spring.config.location parameter is specified, Springboot will only search for the directory set by the parameter, but if the spring.config.additional-location parameter is specified, Springboot first search the parameter setting directory, and then search the above 4 directories.


Special Note:
1. Once the spring.config.location parameter is specified, Springboot will no longer search for the above 4 directories.
2. Even if a parameter file is found in one of the directories, Springboot will still search for and load the parameter file in the other directory. The Springboot is override at the parameter item level, not the parameter file-level override.
3. If the spring.config.location parameter specifies a file, Springboot will not load the Profile-specific variant file, if the spring.config.location parameter specifies a directory, Springboot will still search for and load the Profile-specific variant file.
4. When developing, we usually put application.properties under/src/main/resources/, after packaging it into executable war/jar, the parameter file will be automatically transferred to classpath:/ , so the file will be searched and loaded.


===========================
Variable placeholder in the parameter file
===========================
Variable placeholder of application.properties
App.name=myapp
App.description=${app.name} is a Spring Boot application


===========================
Profile parameter of the parameter file
===========================
# The following is the contents of the Application.properties file.
# where the spring.profiles.active parameter is either written to death or passed through the command line.
Spring.profiles.active=dev

Environments.dev.url=http://dev.example.com
Environments.dev.name=developer Setup
Environments.prod.url=http://another.example.com
Environments.prod.name=my Cool App


===========================
Profile parameter files for different environments
===========================
# The following is the contents of the Application.properties file.
# where the spring.profiles.active parameter is either written to death or passed through the command line.
Spring.profiles.active=dev

# Here are the contents of the Application-production.properties file
Spring.datasource.driver-class-name=com.mysql.cj.jdbc.driver
spring.datasource.url=jdbc:mysql://localhost:3306/db
Spring.datasource.username=root
Spring.datasource.password=root

# Here are the contents of the Application-dev.properties file
Spring.datasource.driver-class-name=org.h2.driver
Spring.datasource.url=jdbc:h2:mem:db;db_close_delay=-1
Spring.datasource.username=sa
Spring.datasource.password=sa

Springboot Series: Java application and springboot parameter files

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.