This will give you an overview of Spring Boost and give you an overview of it.

preface

There has been no update in these days. In my last article, I intended to write about MyBatis and the application of the actual project, but AFTER I read the code of the project, I found that I still couldn’t understand many parts in it, so I stopped writing for now. I will write this content after two months when my ability level is improved.

I’ve been reading about Spring Boost for two days, but it’s just a framework. It’s not easy to write, and you need to use the framework yourself. Since we are going to write this series, we should start from the basics. We are not going to write many chapters in this series, but we can master the basic knowledge, which can be used in the project later, and then combined with specific scenes to learn slowly.

What is SpringBoot?

With the popularity of dynamic languages (Ruby, Groovy, Scala, Node.js), the development of Java is particularly cumbersome, with various configurations, low development efficiency, complex deployment process and difficult integration of third-party technologies.

In the above environment, Spring Boot came into being. It uses the idea of “habit over configuration” (there’s a lot of configuration in the project, plus a custom configuration built in so you don’t have to do it manually) to get your project running quickly.

It’s easy to create a standalone (running JAR, embedded Servlet container), production-level project based on the Spring framework with Spring Boot, and you can use Spring Boot with little or no Spring configuration.

Spring Boot core features

  1. Standalone Spring projects: Spring Boot can be run independently as jar packages. Running a Spring Boot project requires only Java -jar xx.jar to run.
  2. Embedded Servlet Container: Spring Boot has the option of embedding Tomcat, Jetty, or Undertow so we don’t have to deploy the project as a WAR package.
  3. Provide starter to simplify Maven configuration: Spring provides a series of starter POMs to simplify Maven dependency loading. For example, when you use Spring-boot-starter-Web, dependency packages are automatically added.
  4. Automatic Configuration of Spring: Spring Boot automatically configates beans for classes in jar packages based on the classes in the classpath, which greatly reduces the configuration we need to use. Of course, Spring Boot only considers most development scenarios, not all of them, and if we need to automatically configure beans in actual development and Spring Boot does not support this, we can customize the automatic configuration.
  5. Quasi-production application monitoring: Spring Boot monitors running projects based on HTTP, SSH, and Telnet.
  6. No code generation and XML configuration: The magic of Spring Boot is achieved not through code generation, but through conditional annotations, a new feature in Spring 4.x. Spring 4.x advocates the use of a combination of Java configuration and annotation configuration, while Spring Boot implements all of Spring’s configurations without any XML configuration.

Advantages and disadvantages of Spring Boot

Advantages:

  • Build projects quickly.
  • Configuration-free integration with mainstream development frameworks.
  • Projects can run independently without external dependencies on the Servlet container.
  • Provides runtime application monitoring.
  • Greatly improve the efficiency of development and deployment.
  • Natural integration with cloud computing.

Disadvantages:

  • Version iteration speed is very fast, some modules change a lot.
  • Because you do not have to configure yourself, it is difficult to locate errors.

Three-body structure parsing of @SpringBootApplication annotations

@SpringBootApplication

We can see the Boot class for Spring Boot:

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

@SpringBootApplication is a three-body structure that is actually a composite Annotation:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScanpublic
@interfaceSpringBootApplication{... }Copy the code

Although its definition uses multiple annotations to annotate meta information, in fact, only three annotations are important to the SpringBoot application, and the “three-body” structure actually refers to these three annotations:

  • @Configuration
  • @EnableAutoConfiguration
  • @ComponentScan

So, if we use the following SpringBoot boot class, the whole SpringBoot application will still function as the previous boot class:

@Configuration
@EnableAutoConfiguration
@ComponentScan
class DemoApplication {
    public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code

@Configuration

For specific usage, please refer to the article “[Spring Basics Series 3] Common Notes of Spring”.

Many SpringBoot code examples like to label the boot class @Configuration or @SpringBootApplication directly, which is confusing to developers who are new to SpringBoot. If we split the above SpringBoot boot class into two separate Java classes, the whole picture becomes clear:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class DemoConfiguration {
    @Bean
    public Controller controller(a) {
        return newController(); }}public class DemoApplication {
    public static void main(String[] args) { SpringApplication.run(DemoConfiguration.class, args); }}Copy the code

So, the launcher class DemoApplication is really just the launcher class of a standard Standalone type Java program called the Main function, nothing special. The DemoConfiguration definition of the @Configuration annotation is actually a normal IoC container Configuration class in JavaConfig form.

@EnableAutoConfiguration

@enableAutoConfiguration is not really “creative”, but it can be briefly described as collecting and registering bean definitions for specific scenarios with the help of @import:

  • @enablescheduling loads Spring scheduling-framework related bean definitions into the IoC container via @import.
  • @enablembeanExport loads jMX-related bean definitions into the IoC container via @import.

@enableAutoConfiguration loads all auto-configured bean definitions into the IoC container with the help of @import, and that’s it!

The key information is as follows:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interfaceEnableAutoConfiguration {... }Copy the code

One of the most critical is @ Import (EnableAutoConfigurationImportSelector. Class), with the aid of EnableAutoConfigurationImportSelector, @enableAutoConfiguration helps SpringBoot applications load all eligible @Configuration configurations into the IoC container that SpringBoot is currently creating and using. Just like an octopus (see Figure 1).

With the support of SpringFactoriesLoader, a native tool class of the Spring framework, @enableAutoConfiguration can “intelligently” automate the configuration.

@ComponentScan

For specific usage, please refer to the article “[Spring Basics Series 3] Common Notes of Spring”.

@ComponentScan is optional, because in principle, as the “old revolutionary” of the Spring framework, @ComponentScan’s function is to automatically scan and load qualified component or bean definitions, and eventually load those bean definitions into the container. Loading bean definitions into Spring’s IoC container, we can manually register them individually, not necessarily through batch automatic scanning, so @ComponentScan is optional.

If our current application does not have any bean definitions that need to be loaded via @ComponentScan into the IoC container used by the current SpringBoot application, then, without the @ComponentScan declaration, The current SpringBoot application can still run as usual, functional equivalence.

Springapplication.run executes the process

This is a bit too much, put in the article “[Spring Boot series 1] article with you to understand the Spring Boot (ii)” to explain.

The configuration file

In the article “MyBatis integration with Spring Boost”, we actually use the Spring Boost configuration, which is described in detail here.

YAML vs XML

SpringBoot uses a global configuration file with a fixed configuration file name (available in two forms) :

  • application.properties
  • application.yml

The configuration file is used to: modify the default SpringBoot automatic configuration. (SpringBoot is automatically configured for us at the bottom)

YAML: Data-centric, more suitable for configuration files than JSON, XML, etc. Previous configuration files; Most use the XXXX.xml file.

XML examples:

<server>
  <port>8080</port>
</server>
Copy the code

YAML: configuration example

server:
  port: 8080
Copy the code

YAML basically uses postures

Here’s an example:

Person: lastName: hello Age: 18 boss: false birth: 2017/12/12 Maps: {k1: v1,k2: 12} Lists: lisi ‐ zhaoliu dog: name: The dog age: 12Copy the code

“LastName: hello” is k:v format, and “maps: {k1: v1,k2: 12}” is k:v format, which can also be written as:

maps: 
  k1: v1
  k2: 12
Copy the code

For lists below, an array is represented:

Lists: ‐ Lisi ‐ ZhaoliuCopy the code

It can also be expressed in lines as lists:[lisi,zhaoliu].

Configuration file injection is explained later.

Afterword.

The next article will continue to introduce the basics of Spring Boot, mainly about the “SpringApplication.run execution process”.

This two days see Spring Boot, feel or some abstract, several series is not like before, more tangible things (that is, they can begin to try), then you can try, feeling a bit deep, main is don’t need, also don’t want to spend the time and energy, may be new to this.

First do not want so much, the basic knowledge of the first master, and then learn while watching.