Briefly talk about springboot (getting started)

Time:2021-10-14

SpringBoot

Using spring boot, it is easy to create a spring framework based project that runs independently (running jar, embedded servlet container) and quasi production level. Using spring boot, you can use spring boot without or with little spring configuration.
Springboot is the product of the spring framework’s best practice of the concept of “convention over configuration”. A typical springboot application is actually an application based on the spring framework

Spring boot core functions

1) Spring project running independently

Spring boot can run independently in the form of jar package. To run a spring boot project, you only need to run it through Java – jar xx.jar.

2) Embedded servlet container

Spring boot can choose to embed tomcat, jetty or undertow, so we don’t need to deploy the project in the form of war package.

3) Provide starter to simplify Maven configuration

Spring provides a series of starter POMS to simplify Maven’s dependency loading. For example, when you use spring boot starter web, you will automatically add the corresponding dependency package.

4) Auto configure spring

Spring boot will automatically configure beans for the classes in the jar package according to the jar packages and classes in the class path, which will greatly reduce the configuration we need to use. Of course, spring boot only considers most development scenarios, not all scenarios. If we need to automatically configure beans in actual development, but spring boot does not provide support, we can customize the automatic configuration.

5) Application monitoring of quasi production

Spring boot provides monitoring of runtime projects based on HTTP, SSH and telnet.

6) No code generation and XML configuration

The magic of spring boot is not realized by code generation, but by conditional annotation, which is a new feature provided by spring 4. X. Spring 4. X advocates the combination of Java configuration and annotation configuration, and spring boot does not need any XML configuration to realize all spring configurations.

Advantages and disadvantages of springboot

1) Advantages

  • Quick build project
  • The configuration of mainstream development frameworks cannot be integrated
  • Projects can run independently without external dependency on servlet containers
  • Provide application monitoring at runtime
  • It greatly improves the efficiency of development and deployment
  • Natural integration with cloud computing

2) Shortcomings

  • The iteration speed of the version is very fast, and some modules have changed greatly.
  • Because you don’t need to make your own configuration, it’s difficult to locate when an error is reported.
  • There are few ready-made solutions on the Internet.

Create a springboot project

I won’t say much about the project creation process. Idea or TST is easy to create. After the project is created successfully, we will get a startup class of springboot application, as shown in the following code:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
   public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
   }
}

As you can see, @ springbootapplication is a “three body” structure. In fact, it is a composite annotation:

@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 {
    ...
    ...
    ..
}

Although its definition uses multiple annotations for meta information annotation, in fact, for springboot applications, only three annotations are important, and the “three body” structure actually refers to these three annotations:

  • @Configuration
  • @EnableAutoConfiguration
  • @ComponentScan

Therefore, if we use the springboot startup class marked with the above three annotations, the whole springboot application can still have the same function as the previous startup class, but it is obviously too cumbersome to write three annotations each time, so it is obviously more convenient to write a one-stop composite annotation such as @ springbootapplication.