There are three ways to create a Spring Boot project. Each of these three ways is referenced in the pom.xml coordinate file:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4. The RELEASE</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
Copy the code
Do you fully understand what the parent does? The dependency version number is defined in the parent file. Yes, that’s true, but parent is more than that. Let’s talk about what the parent does.
The basic function
When we create a Spring Boot project, we may or may not inherit from a spring-boot-starter-parent, but let’s look at the first case. What are the basic functions of parent?
- The Java compiled version is defined as 1.8.
- Encoding in UTF-8 format.
- Inherited from
spring-boot-dependencies
“, which defines the version of the dependency. Because we inherit the dependency, we do not need to write the version number when writing the dependency. - Perform the configuration of the packaging operation.
- Automated resource filtering.
- Automated plug-in configuration.
- Resource filtering for application.properties and application.yml, including configuration files for different environments defined by profiles, For example, application-dev.properties and application-dev.yml.
Note that since application.properties and application.yml files accept Spring-style placeholdersThe ${... }
, so Maven filtering changes to use@.. @
Placeholders, which developers can override, of course, by setting the Maven property named resource. Delimiter@.. @
Placeholder.
Source code analysis
When we create a Spring Boot project, we can see the specific parent file in the local Maven repository. For example, with version 2.1.4, The path here in Songo is C: \ Users \ sang \. M2 \ repository \ org \ springframework/boot/spring – the boot – starter – parent \ 2.1.4 RELEASE \ spring – the boot – starter – paren T-2.1.4.release.pom, open this file, quickly read the source file, basically can confirm the function we said before, as shown below:
In addition to the spring-boot-Dependencies dependencies, we can also see the project encoding format, JDK version, and data filtering information. Spring-boot-dependencies = “spring-boot-dependencies”; spring-boot-dependencies = “spring-boot-dependencies”;
Here we see the version definition and dependencyManagement node, and understand why some dependencies in Spring Boot projects do not need to write version numbers.
Don’t use the parent
However, not all companies need this parent. Sometimes, the company has its own definition of parent, and our Spring Boot project needs to inherit from the parent. What should we do in this case?
An easy way to do this is to define the version number in the dependencyManagement node and then refer to the dependency without writing the version number, as follows:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4. The RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Copy the code
Once you’ve done this, the dependency number problem is resolved, but the configuration of the packaged plug-in, the compiled JDK version, the encoding format of the file, and so on, all have to be configured in the absence of parent.
conclusion
Spring Boot is a Spring Boot project in which the parent functions.
Pay attention to the public account [Jiangnan little Rain], focus on Spring Boot+ micro service and front and back end separation and other full stack technology, regular video tutorial sharing, after attention to reply to Java, get Songko for you carefully prepared Java dry goods!