SpringBoot core ① - getting started

SpringBoot core ① - getting started

Based on the compilation of teacher Lei Fengyang's tutorial

brief introduction

A framework to simplify Spring application development;

A large integration of the whole Spring technology stack;

One stop solution developed by J2EE;

Baidu Encyclopedia


  • Quickly create independent Spring projects and integrate with mainstream frameworks
  • Using the embedded Servlet container, the application does not need to be packaged into a WAR package
  • starters automatic dependency and version control
  • A large number of automatic configurations can simplify development, and the default values can also be modified
  • No XML configuration, no code generation, out of the box
  • Runtime application monitoring in quasi production environment
  • Integration with natural cloud computing


2014,martin fowler

Microservices: architecture style (service miniaturization)

An application should be a group of small services; Interworking can be carried out through HTTP;

Single application: ALL IN ONE

Microservices: each functional element is ultimately a software unit that can be replaced and upgraded independently;

Refer to the microservice documentation for details

Going to a restaurant for dinner is a complete business. The chefs, garnishes, waiters and waiters of the hotel are distributed; There is more than one chef, garnish, waiter and waiter, which is the cluster; Distributed is a form of micro service. Distributed is the deployment level and micro service is the design level

Environmental constraints

–jdk1.8: Spring Boot recommends jdk1 7 and above;

–maven3.x: Maven version 3.3 or above;

Maven settings

Settings for maven Add the profiles tag of the XML configuration file: (set the jdk version to use)

maven in the development tool is set as the maven configured by yourself

</profile>`Copy to clipboardErrorCopied

Create a maven project

  1. Import spring boot related dependencies

        </dependencies>`Copy to clipboardErrorCopied
  2. Write a main program; Start the Spring Boot application

    `package cn.clboy.springboot;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
     * @Author cloudlandboy
     * @Date 2019/11/13 2 pm:58
     * @Since 1.0.0

public class HelloWorldMainApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldMainApplication.class, args);
}`Copy to clipboardErrorCopied

  1. Write a Controller

    `package cn.clboy.springboot.controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
     * @Author cloudlandboy
     * @Date 2019/11/13 3 pm:05
     * @Since 1.0.0

public class HelloController {

    public String hello(){
        return "hello SpringBoot,this is my first Application";
}`Copy to clipboardErrorCopied
  1. Run the Main method test of the Main program
  2. visit localhost:8080/hello

Simplify deployment

  1. Add maven plug-in

     `<!-- This plug-in can package the application into an executable jar Package;-->
        </build>`Copy to clipboardErrorCopied
  2. Packaging with mvn package
  3. Enter the directory where the packaged jar package is located
  4. Run with the Java - jar package name

Hello World exploration

rely on

 `<!--Hello World The parent project of the project is org.springframework.boot-->

        org.springframework.boot His parent project is spring-boot-dependencies
        He came to really manage Spring Boot All dependent versions in the application;
        Spring Boot The version of the Arbitration Center;
        In the future, we will import dependency. By default, we don't need to write a version; (not in) dependencies The dependency managed in it naturally needs to declare the version number)
  </parent>`Copy to clipboardErrorCopied


        </dependency>`Copy to clipboardErrorCopied


Spring boot starter: spring boot scenario initiator; Help us import the components that the web module depends on for normal operation;

Spring Boot extracts all functional scenarios and makes them into starters. It only needs to introduce these starters into the project, and all dependencies of relevant scenarios will be imported. You can import the initiator of any scene with any function you want

Main program class

public class HelloWorldMainApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldMainApplication.class, args);
}`Copy to clipboardErrorCopied

@SpringBoot application: the spring boot application is marked on a class to indicate that this class is the main configuration class of SpringBoot. SpringBoot should run the main method of this class to start the SpringBoot application;

Take a look at the source code of @ SpringBootApplication annotation class

`@Target({ElementType.TYPE})    //You can annotate a type, such as class, interface and enumeration
@Retention(RetentionPolicy.RUNTIME)    //It can be kept until the program runs, and it will be loaded into the JVM
@Documented    //Include the elements in the annotation into Javadoc.
@Inherited    //Inheritance. For example, class A has the annotation, class B inherits class A, and class B also has the annotation



*Create a configuration class and add @ ComponentScan annotation on the configuration class.
*This annotation will scan all configuration classes under the package where this class is located by default, which is equivalent to the previous < context: component scan >.
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
public @interface SpringBootApplication`Copy to clipboardErrorCopied
  • @SpringBootConfiguration: configuration class of Spring Boot; Marked on a class, indicating that it is a Spring Boot configuration class;

    public @interface SpringBootConfiguration`Copy to clipboardErrorCopied
    • @Configuration: mark this annotation on the configuration class;

      Configuration class - configuration file; The configuration class is also a component in the container@ Component

      public @interface Configuration` Copy to clipboardErrorCopied
  • @EnableAutoConfiguration: enable the auto configuration function;

    Spring Boot helps us configure things we need to configure before@ EnableAutoConfiguration tells SpringBoot to enable the auto configuration function; Only in this way can the automatic configuration take effect;

    public @interface EnableAutoConfiguration`Copy to clipboardErrorCopied
    • @AutoConfigurationPackage: autoconfiguration package

      public @interface AutoConfigurationPackage`Copy to clipboardErrorCopied
      • @Import: Spring's bottom annotation @ import imports a component into the container

        Imported components by`org.springframework.boot.autoconfigure.AutoConfigurationPackages.Registrar`Configure the main configuration class(@SpringBootApplication All components in the package and all sub packages below the marked class) are scanned to Spring Containers;

        here controller The package is under the package where the main program is located, so it will be scanned. We springboot Create one under package test Package, put the main program in test Under the package, so the startup will only scan test Content under package controller The package will not be scanned before accessing the beginning hello It's 404
    • @Import({AutoConfigurationImportSelector.class})

      AutoConfigurationImportSelector.class returns all components to be imported in the form of full class name; These components will be added to the container; Many automatic configuration classes (xxxAutoConfiguration) will be imported into the container; Import all the components needed for this scenario into the container and configure them;

      The automatic configuration class eliminates the need for us to manually write configuration injection function components;

==Spring Boot starts from meta-inf / spring. In the classpath Obtain the values specified by EnableAutoConfiguration from factories, import these values into the container as automatic configuration classes, and the automatic configuration class will take effect to help us with automatic configuration== In the past, we needed to configure things ourselves, and the automatic configuration class helped us;

The overall integration solution and automatic configuration of J2EE are in spring-boot-autoconfigure-1.5.9 RELEASE. jar;

Use Spring Initializer to quickly create Spring Boot projects

IDE supports the use of Spring's project creation wizard to quickly create a Spring Boot project;

Select the module we need; The wizard will create a Spring Boot project online;

Need networking


  1. Select Spring Initializr when creating the project

  2. Improve project information

    The reason for the appearance of Artifact contains illegal characters is that uppercase is used in Artifact, which can only be all lowercase, and words are separated by -

  3. Select the desired starter

  4. After the creation, the unwanted files can be deleted. The xml file has automatically configured the dependencies for us. We can configure whatever we have selected before


  1. Using STS
  2. When creating a project, select Spring Starter Project

  3. Perfect information

  4. Select the desired starter

Spring Boot project generated by default

  • The main program has been generated. We just need to complete our own logic
  • Directory structure in resources folder

    • Static: save all static resources; js,css,images;
    • templates: save all template pages; (the default jar package of Spring Boot uses embedded Tomcat and does not support JSP pages by default); You can use template engines (freemaker, thymeleaf);
    • application.properties: configuration file of Spring Boot application; You can modify some default settings;

Tags: Java Spring Boot

Posted by morris520 on Mon, 16 May 2022 12:06:29 +0300