Summary of springboot knowledge points

1. Summary of springboot

1 Overview

Springboot is an easy way to quickly use the spring framework. In short, springboot is equivalent to a scaffolding built by a program. Its biggest role is to help us build projects quickly , and to reduce xml configuration files as much as possible, making program development easier Simple, out-of-the-box, and quick to get started , so that we can focus more on the business logic of the program, not the configuration file, in the process of program development. In short, I summarize three words for springboot , summary: light (easy to configure, easy to use, start time period), fast, simple

2. Why use springboot

Because one of the biggest pain points in Java development in the past is that the configuration is particularly bloated and troublesome. Sometimes when you are fortunately struggling to set up the environment, the python programmer may have already finished typing the code (young people don’t talk about martial arts) , mainly because of the following two reasons:

1. Complex configuration

Various configurations in the project are actually a loss during development. Programmers need to switch back and forth between project configuration and business logic in the development process, all of which are a headache for programmers. During the development process, I think that programmers should spend more time and experience on business code, and configure these because they should have time to study them privately.

2. Confused project management

Dependency management in a project is a very difficult thing to do. It would be a headache to choose which libraries to use, but after choosing dependencies, you have to consider the issue of version conflicts. Problems, and the choice is a kind of consumption, and if the choice is wrong, the later rework is a consumption, and it may slow down the progress of the project. All this work is usually done by the main process of the company.

In summary, springboot solves the above problems, can quickly build a development environment, and there are not so many complex configurations, and can manage project dependencies well

3. Features of springboot

1. Simple and lightweight, out of the box, easy to use

2. Fast, the environment is built quickly, there is no cumbersome configuration, and the convention is greater than the configuration

3. Provides some common non-functional features in large-scale projects, embedded server, security, indicators, health detection, etc.

The above are probably some of the main features of springboot

4. Configuration file types supported by springboot

1. .properties (priority loading, the same as the previous one, but the latter is generally used)

2. .yml (indented, generally used, because it can put collection arrays, and it is relatively clear and easy to read)

3. .yaml (same as above)

5. Springboot automatic configuration principle

Springboot has a startup class, which can be explored from this startup class:

There is a key annotation on the startup class: @SpringBootApplication , there is a run method in the main method, springApplication.run() ,

Discuss separately:

1. **SpringBootApplication:** Click to enter, you can see that there are seven key annotations:

  1. Where does the @Target(ElementType.TYPE) annotation take effect, and ElementType.TYPE is on the class
  2. When does @Retention(RetentionPolicy.RUNTIME) take effect, RetentionPolicy.RUNTIME takes effect when it runs
  3. @Documented whether to generate api documentation
  4. Whether @Inherited is inherited
  5. **@SpringBootConfiguration (click into @Configuration)** declares the current class as the configuration class, springboot will automatically scan the class with @Configuration added, and read the configuration information, @SpringBootConfiguration declares the current class as the configuration class of springboot , springboot has one, so we don't need to add it ourselves
  6. Does @EnableAutoConfiguration introduce some third-party libraries, if some dependencies of the third-party libraries are introduced, these configurations will take effect, so after we build the springboot project, if we want to refer to the third-party libraries, we only need to import the corresponding dependencies, and the configurations are all Hand it over to springboot for processing
  7. @ComponentScan is a function of scanning packages. Because this annotation is added to the startup class of springboot, the scanned packages are packages and subpackages of the same level. Therefore, the general startup class will be placed in a package directory before the comparison. .

Trigger automatic configuration: introduce the corresponding starter dependencies, introduce condition-specific classes, and then start automatic configuration

6. Default configuration principle

We can follow the run method in the SpringApplication.run method, then follow up, then follow up with SpringFactoriesLoader, and finally find spring.factories, so after springboot is initialized, load all classpath:META-INF/spring .factories file, and in a dependency package of Spring: spring-boot-autoconfigure, there is such a file. As long as we introduce third-party configuration, there will be similar files.

  • @Configuration: declares that this class is a configuration class

  • @ConditionalOnWebApplication(type = Type.SERVLET)

    ConditionalOn, translation is under a certain condition, here is the class that satisfies the project is Type.SERVLET type, which is a common web project, obviously we are

  • @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })

    The condition here is OnClass, that is, the following classes exist: Servlet, DispatcherServlet, WebMvcConfigurer, of which Servlet will naturally exist as long as tomcat dependencies are introduced, and the latter two need to introduce SpringMVC. This is to determine whether you have introduced relevant dependencies. After the dependencies are introduced, the configuration of the current class will take effect!

  • @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

    This condition is different from the above. OnMissingBean means that the Bean that is not specified in the environment will take effect. In fact, this is the entry of custom configuration, that is to say, if we configure a WebMVCConfigurationSupport class ourselves, then this default configuration will be invalid!

Summarize

SpringBoot provides us with a default configuration, and the steps for the default configuration to take effect:

  • The @EnableAutoConfiguration annotation will look for the fileMETA-INF/spring.factories and read the names of all the classes in it as keys. These classes are the automatic configuration classes written in advanceEnableAutoConfiguration
  • These classes are declared with @Configurationannotations, and @Beanall the instances we need are configured in advance through the annotations. Complete automatic configuration
  • However, these configurations do not necessarily take effect, because there are @ConditionalOnannotations, they will take effect only when certain conditions are met. For example, one of the conditions: some related classes must exist
  • For the class to exist, we only need to introduce the relevant dependencies (starters), and when the dependencies are established, the automatic configuration takes effect.
  • If we configure related beans ourselves, we will override the default auto-configured beans
  • We can also override the properties in the auto-configuration by configuring the application.properties file

1) Launcher

Therefore, if we don't want to configure, we only need to introduce dependencies, and we don't need to worry about the dependency version, because as long as the stater (starter) provided by SpringBoot is introduced, the dependencies and versions will be automatically managed.

Therefore, the first thing to play with SpringBoot is to find a starter. SpringBoot provides a large number of default starters

2) Global configuration

In addition, the default configuration of SpringBoot will read the default properties, and these properties can be overridden by custom application.propertiesfiles. In this way, although the default configuration is still used, the value in the configuration is changed to our custom.

Therefore, the second thing to play with SpringBoot is application.propertiesto form a custom configuration by overriding the default property values. We need to know the default property key of SpringBoot, there are many, it can be automatically prompted in idea

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324092995&siteId=291194637