SpringBoot: introduction, simple SpringBoot examples and quick creation of SpringBoot projects

Posted by and1c on Mon, 17 Jan 2022 08:59:22 +0100

1, Introduction to SpringBoot

1. What is springboot?

  • Production background: Spring development has become more and more cumbersome, with a large number of xml files, cumbersome configuration and complex deployment process. It is difficult to integrate the third-party framework, resulting in low development efficiency (the early framework is too complex). Therefore, in this case, SpringBoot was developed!

2. Why use SpringBoot

SpringBoot is a framework used to simplify the initial creation and development process of Spring applications, simplify configuration and realize rapid development

  • Integrating the entire Spring technology line,
  • It can simply create an independent Spring project and integrate with the mainstream framework
  • Built in Servlet container, the application does not need to be packaged into a war package
  • Use Starter to manage dependencies and version control
  • A large number of automatic configurations to simplify development
  • Provide runtime monitoring of quasi production environment, such as indicators, health inspection and external configuration
  • There is no need to configure xml and no redundant code generation, that is, out of the box.

2, First SpringBoot program

There are many versions of SpringBoot. At present, the latest version of SpringBoot is based on 5.0

1. Requirements of springboot for running environment

  • SpringBoot2.0 (based on springboot 5.0)
  • The JDK version must be above 1.8
  • The Tomcat server must be a server version above 8.5
  • Maven version: version above 3.2
  • IDEA version: try to use the latest version

2. Operation steps

  • Create a Maven Java engineering application

Traditional web applications need to create a web project, package it into a war package later, and then put it into tomcat, which is more complex. The SprinbBoot application only needs to create a Java project, which is directly packaged into a jar package later, and can be loaded directly by Tomcat

  • Add dependent files
  1. Add the parent POM (spring boot starter parent)
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.5.2</version>
    </parent>
  1. Add dependency (spring boot starter WEB)
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.5.2</version>
        </dependency>
  1. Create business function classes, such as service,ado,controll, etc
@Controller
public class UserActionImpl {

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello";
    }
}
  1. Write main program class
@SpringBootApplication
@ComponentScan("ado.impl")
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}
  • be careful:
    When scanning the business processing layer, control layer and other layers without specifying the location with annotations, they can only be placed in the package where the main program class is located or its sub package. If they are not in these locations, they can only specify the location where the package needs to be scanned with annotations: @ ComponentScan("ado.impl")
  1. Deployment packaging
    To deploy packaging, you need to add a packaging plug-in
    <plugin>
        <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <version>2.5.2</version>
     </plugin>

3. Analytical engineering (HelloWorld)

3.1 dependency package management

  • Parent project: spring boot starter parent;
<!-- TODO:add to spring-boot Basic dependency, he basically includes spring All basic packages -->
<parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.5.2</version>
</parent>

  • Parent project in parent project: spring boot dependencies
    Spring boot dependencies: all dependent packages and corresponding versions in the specified project

Specify the corresponding dependencies through the starter

<!-- TODO:spring-boot Other packages that need to be enhanced can be placed in dependecies In, the following is web Packages required for development -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.5.2</version>
</dependency>

SpringBoot provides many starter dependencies corresponding to different functional scenarios. When these stators are introduced into the project, the dependencies required by the corresponding scenarios will be imported, that is, the starter dependency contains the sub dependencies of the corresponding scenarios!

3.2 main configuration annotation, configuration annotation and

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {

Key understanding: the main configuration class and configuration class can be regarded as an IOC or AOP container. The main configuration class and configuration class are determined by the @ springbootapplicatorin and SpringBootConfiguratin annotations. If other beans can be configured in it, then this class can be understood as configuration class (container class)

  • @SpringBootApplication
    This annotation is marked on the class to indicate that this class is the main configuration class of SpringBoot (it can be understood that if it is actually a configuration class, this class is also an IOC or AOP container). Run the main method of this class to start the application of SpringBoot.
    Analyze the @ SpringBootApplication annotation, and there is a @ SpringBootConfiguration annotation on it.

  • @SpringBootConfiguration
    This annotation is also marked on the class, indicating that this class is a SpringBoot Configuration class. There are many Configuration classes, but there is only one main Configuration class: @ SpringBootConfiguration. Above this comment, there is still a comment: @ Configuration

  • @Configuration
    It is marked on the class, indicating that this class is the configuration class of Spirng, which is equivalent to an xml file

  • @EnableAutoConfiguration
    When the auto configuration function is enabled, SpringBoot will automatically complete many configurations, simplifying the previous cumbersome configuration.
    When SpringBoot starts, meta-inf / spring. Inf will be in the classpath Obtain the values specified by EnableAutoConfiguration in factories, and add these values as automatic configuration classes to the container. These automatic configuration classes will help us complete multi line configuration. (personal understanding: automatic configuration can be considered as automatically creating spring in the main configuration class (IOC or AOP container) For the class beans specified in the factories file, let these specified class beans be created in the container of the main configuration class or other configuration classes.)

  • @ComponentScan
    Mark on the class and specify the package to scan.

3, Quickly create a SpringBoot project

1. Introduction

  • Use Spring Initializr to quickly create a SpringBoot project, provided that you need to be connected to the network!

2. Basic operation

  • POM files and main program classes have been generated, and business logic code can be written directly
  • Directory structure of the resource folder
|-staic  Used to store static resources, such as class,js,image
|-templates  To store template pages, you can use a template engine, such as freemarker,thymelerf etc.
|-application.properties SpringBoot Some default settings can be modified according to the applied configuration file 

3. Create process (illustration example)




Topics: Java Maven Spring Spring Boot