springboot series (three) Detailed Profile

Introduction:
Springboot have a global configuration file, the default configuration file is a properties file, the file is application.properties, in fact, there is a file, the file is yml, but this file is necessary to create yourself. This configuration file is used to modify the default configuration of Springboot. For example, when we integrate the start-web, Springboot default web server Tomcat, we can do some processing on the default configuration of Tomcat. Profiles can do a lot, the syntax is simple.

properties file

1. Grammar

key=value

For example the port configuration Tomcat: server.port = 8081
case sensitive.

2. Priority

In general, properties can be in four local projects exist, then each location they have different priorities, and the priority when application.properties different names is the highest, that is, Springboot start when he scans own designated path following application.properties file as the default configuration file.
Package path scanning order of priority has the following four:

  1. file:./config/
  2. file:./
  3. classpath:/config/
  4. classpath:/

That is to say, you may not know where to look Zhang map, the first project is the root directory of the config file below, the second is the root directory, and the third is the resources file the following config file below, the last one is the following resources file.

Here Insert Picture Description

According to this priority, Springboot will start when scanning these packages, then configure the implanted spring environment, if you encounter the same configuration, high priority overrides lower priority configuration, for example, you have four files, four documents which are equipped with a port project started, this configuration will only be used to configure the port with the highest priority that file. Encounter different configurations that will complement each other, forming a maximized configuration.

We can also change the default configuration file location by spring.config.location. But you need to package the project, this one to keep the back again, I will continue to update! You can also wx search full stack study notes! Push Essay wonderful day!

3. The custom configuration data

In addition to modifying configuration files inherent in some of the original configuration, but also custom configuration.
Custom object data:

Here Insert Picture Description
Define a student object, the object which contains the common data types.

Custom entity classes:

Here Insert Picture Description

If you encounter java hump named file named properties inside is more relaxed, such as java inside with studentName, I can either use studentName in properties which can also be used student-name, or student_name, will do.
This is customize the configuration data. Let's see how to obtain these data.

4. Get custom configuration data

In the configuration file, some configuration is springboot be obtained directly, some can not be obtained, such as custom configuration, such as server.port originally belonging to some of the default configuration, we do modify. If we have some custom configuration, some of the data we have defined as above, we need to get the other way.

properties: string Default do not add a single or double quotes
'' single quotation marks, will escape special characters
"" double quotes will not escape special characters
yml is the same

Project structure:

Here Insert Picture Description

1. Get through the prefix

Entity class code:

Here Insert Picture Description

Description: The binding properties of this entity inside the student to the class above, it will be one to one, the source code should be used for reflection and annotations, reflections, annotations can not look at my previous article, oh! They are dry.

controller inside the test code:

Here Insert Picture Description
Re-run after the code filled out and look at your environment to run the port, the port is 8086 my run
Here Insert Picture Description
in the browser input: HTTP: // localhost: 8085 / Student / getStudent

Then you can see to get the data, access to data:

Here Insert Picture Description

But you'll get to see this data to the Chinese is garbled, which need to set up the idea. file-> settings

Here Insert Picture Description

Set completion point apply, such as if the original Chinese is garbled, then you need to change it back, and then restart the project will not be garbled.

2. Get notes by @value

Here Insert Picture Description

The results thus obtained show that only a name, $ is a placeholder, the use of this grammar, the need for each entity class properties correspond much trouble. The first method is recommended.

yml file

1. Grammar

key space: space value

Note: Space and no less, case sensitive
use of indentation on behalf of hierarchy, as long as it is indented on behalf of a level

2. Priority

In both yml file also has properties file, when (in fact, there is a yaml, and yml almost), yml loading sequence is preceded properties, so the priority is greater than yml properties, then the priority and properties in other cases file is the same.

3. The custom configuration data.

Custom data files and properties similar. A little bit of difference in wording, but its function is relatively strong, the code is simple, highly readable.
Here Insert Picture Description

4. Get custom configuration data

1. prefix

Whether yml, or properties file, obtaining the data set is the same usage.

2. Get notes by @value

Ibid way properties acquired file.

Guess you like

Origin www.cnblogs.com/swzx-1213/p/12641842.html