Simple introduction to springboot (1): Introduction to SpringBoot

1. Introduction to SpringBoot

Spring Boot makes it very easy to develop independent, product-level Spring-based applications, just "just run". We provide out-of-the-box settings for the Spring platform and third-party libraries so that you can start in an orderly manner. Most Spring Boot applications require very little Spring configuration.

You can use Spring Boot to create a Java application, and start it with java -jar or use the traditional war deployment method.

1.1 Problem solved
  1. Too many dependencies, and there are version problems
  2. There are too many configurations and the same every time. For most projects, the configuration is the same every time, copied from one place to another. And Spring has developed for more than 10 years, and there are too many configuration versions for many programmers. , It is not clear which is valid and which is invalid.
  3. Deployment is too troublesome. Need tomcat deployment, project structure also needs to follow the Java EE directory structure to write.
1.2 SpringBoot features
  1. Create a standalone Spring application
  2. Embedded Tomcat, no need to deploy WAR file
  3. Simplify Maven configuration
  4. Automatic configuration of Spring
  5. Provide production-ready functions such as indicators, health checks and external configuration
  6. Absolutely no code generation and no configuration requirements for XML

1.3 SpringBoot function

  • Auto-configuration is
    a function that simplifies configuration. For example, if a spring security jar package is found in the classpath, related beans will be automatically created.
  • Starters (simplified dependencies) are
    more critical, which is convenient for spring to integrate various components, such as redis, mongodb, and so on.

1.4 Development of SpringBoot

Insert picture description here

2. System requirements

By default, the latest version of SpringBoot 2.1.2 is used. It is best to install JDK8 and above, and maven uses 3.3 or above (this article uses maven3.5)

3. The first SpringBoot project

Step 1: Create a new ordinary maven project:

Step 2: Create a pom file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>enjoy</groupId>
    <artifactId>springbootvip</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

Step 3: Write the code

In order to complete the application, we need to create a separate Java file. Maven will compile the source code under src/main/java by default.
New: cn.enjoy.Example

package cn.enjoy;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
    
    
    @RequestMapping("/")
    String home() {
    
    
        return "Hello SpringBoot!";
    }
    public static void main(String[] args) throws Exception {
    
    
        SpringApplication.run(Example.class, args);
    }
}

Tips:
@RestController and @RequestMapping Description:
@RestController : This is called a stereotype annotation. It provides suggestions for people reading the code. For Spring, this class plays a special role. In this example, our class is a web @Controller, so when an incoming web request is processed, Spring will ask it.
@RequestMapping : The annotation provides routing information. It tells Spring that any HTTP request from the "/" path should be mapped to the home method. The @RestController annotation tells Spring to render the result as a string and return it directly to the caller.
@EnableAutoConfiguratio : This annotation tells Spring Boot to guess how you want to configure Spring based on the added jar dependencies. Since spring-boot-starter-web adds Tomcat and Spring MVC, auto-configuration will assume that you are developing a web application and set up Spring accordingly.
Main method : This is just a standard method, it follows the Java convention for the entry point of an application. Our main method delegates the business to the Spring Application class of Spring Boot by calling run. SpringApplication will guide our application, start Spring, and start the automatically configured Tomcat web server accordingly. We need to pass Example.class as a parameter to the run method to tell SpringApplication who is the main Spring component.

执行main方法,使用一个浏览器打开?localhost:8080,以下输出:
Hello World!

4. Matters needing attention

Spring Boot does not need to use any special code structure, however, there are some places to pay attention to using the "default" package.

When a class does not contain a package declaration, it is considered to be under the default package. The default package is generally not recommended and should be avoided. Because for Spring Boot applications annotated with @ComponentScan, @EntityScan or @SpringBootApplication, the classes from each jar will be read, which will cause certain problems.

Locate the main application class It
is usually recommended that you put the main application class in the root package above other classes. Annotate your main class with @EnableAutoConfiguration and implicitly define a basic "search package" for some items.
For example, if you are writing a JPA application, the package containing the class annotated by @EnableAutoConfiguration will be used to search for @Entity items.

Using the root package allows you to use the @ComponentScan annotation without the need to define a basePackage attribute. If the main class is in the root package, you can also use the @SpringBootApplication annotation.

The following is a typical structure:

cn
 +- enjoy
     +- myproject
         +- Application.java
         |
         +- domain
         |   +- Customer.java
         |   +- CustomerRepository.java
         |
         +- service
         |   +- CustomerService.java
         |
         +- web
             +- CustomerController.java

Next chapter: Simple introduction to springboot (two): springboot environment construction

Guess you like

Origin blog.csdn.net/weixin_46822085/article/details/109228347