In the previous article, we experienced Spring Boot. In this article, we briefly explain the example of the previous article.
1. Code structure
The Spring Boot project structure is the basic structure of maven. The startup class is generally placed in the root package of the code. For example com.songguoliang.demo
, the official recommended root package is the reverse domain name.
resources is a resource package, where the required configuration files are placed
Test content is stored in the test
2. pom.xml
<!-- Spring Boot启动器父类 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- Spring Boot web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
In pom.xml
it, unlike the previous ordinary maven projects, there is one more <parent>
, which is the basis of all Spring Boot starters and defines many commonly used dependencies and versions.
Spring Boot defines a lot of starters. When a function needs to be used in the project, the starter of the function can be introduced, and there is no need to manage the dependencies of each jar separately.
Spring Boot official starter can refer to: Spring Boot official starter
The naming method of Spring Boot starters is generally at the spring-boot-starter
beginning, followed by related technologies.
`
3. Entry class and @SpringBootApplication
Spring Boot usually has an *Application
entry class with a main
method in it. To put it bluntly, it is the entry method of a standard java application.
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
The most important part of the entry class is the annotation @SpringBootApplication
, which is a combined annotation, as shown in the following figure
That is to say, it is @SpringBootApplication
equivalent to @SpringBootConfiguration
, @EnableAutoConfiguration
and @ComponentScan
these three annotations.
@SpringBootApplication
Spring's component scanning and auto-configuration features are enabled.