02 springboot features and functions

SpringBoot features

1. Dependency management

pom. The parent project in XML is used to automatically manage version dependencies.

spring-boot-starter-parent Implement dependency management    
<!--Indicate use springBoot-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.4.RELEASE</version>
</parent>
Click to enter spring-boot-starter-parent Parent project of⬇️
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.4.RELEASE</version>
</parent>
It can be seen that almost all the dependent version numbers commonly used in development are declared, so as to realize the automatic version arbitration mechanism

You can also implement dependency management by importing the started scenario launcher during development.

  1. Seeing many similar spring boot starter - *: * indicates a certain scenario.

  2. As long as the starter is introduced, we will automatically introduce all the conventional dependencies of this scenario.

  3. View all the scenarios supported by SpringBoot.

  4. You can define your own starter. All the * - spring boot starters you see are third-party scene starters (naming conventions).

  5. The lowest level dependency of all scenario initiators is spring boot starter.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.3.4.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    

⚠️ After dependency management is implemented, the version number does not need to be written by default. If you have to modify the version number, you can modify it yourself.

2. Automatic configuration

SpringBoot can implement a series of automatic configuration.

  1. Automatically introduce dependency and configure Tomcat server.

  2. Automatically introduce and configure a full set of spring MVC components.

  3. Automatically configure common web functions, such as character coding, and all web development scenarios, such as dispatcher servlet, filter, etc.

  4. ⭐ The components in the package (boot) where the main program is located and all its sub packages will be scanned by default.

    To change the scanning path, add @ SpringBootApplication(scanBasePackages="com.atguigu") or @ ComponentScan in front of the main program to specify the scanning path, which can also be used to expand the default scanning range.

    @SpringBootApplication
     Equivalent to
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan("com.atguigu.boot")
    
  5. Various configurations have default values. The default configuration is mapped to a class (such as MultipartProperties), which will create objects in the container

3. Load on demand

There are many starter s in SpringBoot. The automatic configuration of this scenario will be enabled only when the scenarios are introduced. All the automatic configuration functions of SpringBoot are in the spring boot autoconfigure package

⭐ Spring boot container function

Component addition

The components in SpringBoot are also beans like Spring. Adding beans in traditional Spring generally needs to be configured in the configuration file, while configuring beans in SpringBoot only needs a simple configuration class to realize injection.

@Configuration //Indicates that this is a configuration class, equivalent to a configuration file
public class MyConfig {
    //Configuration class injection is single instance
    @Bean //Add a component to the container. The method name is component id, the return type is component type, and the return value is the instance of the component in the container
    public User user01(){
        return new User("zhangsan", "18");
    }
 
    @Bean("tom") //Re specify component id
    public Pet tomcatPet(){
        return new Pet("tomcat");
    }
}

Test: try to get the component and test whether it is a single instance.

public static void main(String[] args) {
      //1. Return to IOC container
      ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
      //2. Check the components in the container
      String[] names = run.getBeanDefinitionNames();
      for (String name : names) {
          System.out.println(name);
      }
      //3. Get components from container
      Pet tom01 = run.getBean("tom", Pet.class);
      Pet tom02 = run.getBean("tom", Pet.class);
      System.out.println("Configure whether the component injected by the class is single instance:"+(tom01==tom02));
  }

Component dependency

A new annotation attribute has been added in the new version of Spring, @ Configuration(proxyBeanMethods=false/true) is used to indicate the method of proxy Bean.

proxyBeanMethods = trueEnsure that each @ Bean method is called many times and the returned component is single instanceFull mode
proxyBeanMethods = falseThe number of times each @ Bean method is called and the returned component is newly createdLite mode

Component dependency must use Full mode by default to reduce judgment. In other cases, Lite mode is used by default.

//Method of proxy bean
@Configuration(proxyBeanMethods = false) 
public class MyConfig {
	  //Add a component to the container, the method name is component id, the return type is the component type, and the return value is the instance of the component in the container  
	  @Bean
    public User user01(){
        User zhangsan = new User("zhangsan", "18");
        //The user component depends on the Pet component
        zhangsan.setPet(tomcatPet()); //Component dependency can be written directly as component id
        return zhangsan;
    }
 
    @Bean("tom") //Re specify component id
    public Pet tomcatPet(){
        return new Pet("tomcat");
    }
}

SpringBoot other annotations

SpringBoot is closely related to annotations and must be Deep understanding of annotations in Java.

The @ Bean, @ Component, @ Controller, @ Service, @ Repository annotations in the original Spirng can still be used, and @ ComponentScan can be used for package scanning.

@Import can be used to import a component and write it on the class of a component in the container. Spring calls the parameterless constructor of the component to create the component and put it in the container.

There are also a series of Conditional assembly annotations: component injection can only be carried out when the conditions specified by Conditional are met.

SpringBoot does not support using xml configuration to configure injection, but if necessary, you can use @ ImportResource to import the xml configuration file of the original Spring.

@ImportResource("classpath:beans.xml")
public class MyConfig {}

⭐ Configure binding

SpringBoot can use annotations to dynamically bind the Java code and the content in the properties configuration file, and encapsulate them into JavaBean s for ready use. You only need to modify the configuration file to modify the Bean attribute value. The configuration file is bound with the Bean value.

@Component //Only the components in the container can have the powerful functions provided by SpringBoot
@ConfigurationProperties(prefix = "mycar") //Open component configuration: application Middle note prefix in properties
public class Car {...}

Using this annotation, the above code is the same as application The properties configuration files are dynamically bound together.

 mycar.brand=YD
 mycar.price=10000

Tags: Java Spring Spring Boot

Posted by mubeena on Sun, 17 Apr 2022 11:58:54 +0930