Spring Boot Tutorial (2): Spring Boot First Experience Demo Description

In the previous article, we experienced Spring Boot. In this article, we briefly explain the example of the previous article.

1. Code structure

write picture description here

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.xmlit, 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-starterbeginning, followed by related technologies.
`

3. Entry class and @SpringBootApplication

Spring Boot usually has an *Applicationentry class with a mainmethod 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
write picture description here

That is to say, it is @SpringBootApplicationequivalent to @SpringBootConfiguration, @EnableAutoConfigurationand @ComponentScanthese three annotations.
@SpringBootApplicationSpring's component scanning and auto-configuration features are enabled.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326684197&siteId=291194637