SpringBoot高级


1. SpringBoot 原理分析

1.1 Condition

Condition 是在Spring 4.0 增加的条件判断功能,通过这个可以功能可以实现选择性的创建 Bean 操作。
使用时需要实现此接口进行匹配.通过注解Conditional加以判断.

思考:SpringBoot是如何知道要创建哪个Bean的?比如SpringBoot是如何知道要创建RedisTemplate的?

案例:需求

在 Spring 的 IOC 容器中有一个 User 的 Bean,现要求:

  1. 导入Jedis坐标后,加载该Bean,没导入,则不加载

  2. 新建maven工程,导入依赖

 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.3.3.RELEASE</version>
         <relativePath/> <!-- lookup parent from repository -->
     </parent>
     <groupId>com.example</groupId>
     <artifactId>springboot-day02</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <name>springboot-day02</name>
     <description>Demo project for Spring Boot</description>
 
     <properties>
         <java.version>1.8</java.version>
     </properties>
 
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
         <dependency>
             <groupId>redis.clients</groupId>
             <artifactId>jedis</artifactId>
         </dependency>
 		<dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
             <version>1.2.4</version>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
             <exclusions>
                 <exclusion>
                     <groupId>org.junit.vintage</groupId>
                     <artifactId>junit-vintage-engine</artifactId>
                 </exclusion>
             </exclusions>
         </dependency>
     </dependencies>
 
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>
 
 </project>
 
 
  1. 编写SpringApplication
    在这里插入图片描述
 package com.example.springbootday02;
 
 import com.example.springbootday02.domain.User;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.data.redis.core.RedisTemplate;
 
 @SpringBootApplication
 public class SpringbootDay02Application {
 
     public static void main(String[] args) {
 
         ConfigurableApplicationContext run = SpringApplication.run(SpringbootDay02Application.class, args);
 
 //        RedisTemplate redisTemplate = (RedisTemplate) run.getBean("redisTemplate");
 //        System.out.println(redisTemplate);
 
         User user = (User) run.getBean("user");
         System.out.println(user);
     }
 }
  1. 编写ClassCondition
 package com.example.springbootday02.condition;
 
 import org.springframework.context.annotation.Condition;
 import org.springframework.context.annotation.ConditionContext;
 import org.springframework.core.type.AnnotatedTypeMetadata;
 import redis.clients.jedis.Jedis;
 
 public class ClassCondition implements Condition {
     @Override
     public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
         try {
             Class.forName("redis.clients.jedis.Jedis");
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
             return false;
         }
         return true;
     }
 }
 
  1. 编写UserConfig类和User类
   package com.example.springbootday02.domain;
   
   
   public class User {
   
   }
   

@Conditional的使用
作用:根据条件,决定类是否加载到Spring Ioc容器中,在SpringBoot中有大量的运用
应用场景:在一些需要条件满足才是实例化的类中,使用此注解,我曾经在项目中需要根据不同的场景使用不同的mq中间件的时候使用过,在mq的实例化bean上,加上此注解,根据配置文件的不同,来决定这个bean是否加载至ioc容器中。
使用方法
实现Conditional接口, 实现matches方法,看类是否被加载.

package com.example.springbootday02.config;

import com.example.springbootday02.condition.ClassCondition;
import com.example.springbootday02.domain.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

@Configuration
public class UserConfig {

    @Bean("user")
    @Conditional(ClassCondition.class)
    public User getUser(){
        return new User();
    }
}

案例:需求

在 Spring 的 IOC 容器中有一个 User 的 Bean,现要求:

  1. 导入Jedis坐标后,加载该Bean,没导入,则不加载

  2. 将类的判断定义为动态的。判断哪个字节码文件存在可以动态指定。

  3. 定义注解MyConditionOnClass

   package com.example.springbootday02.condition;
   
   import org.springframework.context.annotation.Conditional;
   
import java.lang.annotation.*;
   
   @Target({ElementType.TYPE, ElementType.METHOD})
   @Retention(RetentionPolicy.RUNTIME)
   @Documented
   @Conditional(ClassCondition.class)
   public @interface MyConditionOnClass {
   
       String[] value();
   }
   
   package com.example.springbootday02.condition;
   
   import org.springframework.context.annotation.Condition;
   import org.springframework.context.annotation.ConditionContext;
   import org.springframework.core.type.AnnotatedTypeMetadata;
   import org.springframework.util.MultiValueMap;
   
   import java.util.Map;
   
   public class ClassCondition implements Condition {
       @Override
       public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
   //        try {
   //            Class.forName("redis.clients.jedis.Jedis");
   //        } catch (ClassNotFoundException e) {
   //            e.printStackTrace();
   //            return false;
   //        }
   //        return true;
           Map<String, Object> myConditionOnClass = annotatedTypeMetadata.getAnnotationAttributes(MyConditionOnClass.class.getName());
           try {
               String[] value = (String[]) myConditionOnClass.get("value");
               for (String classes : value){
                   Class.forName(classes);
               }
           } catch (ClassNotFoundException e) {
               e.printStackTrace();
               return false;
           }
           return true;
       }
   }
   
  1. UserConfig 使用 MyConditionOnClass注解
      package com.example.springbootday02.config;
      
      import com.example.springbootday02.condition.ClassCondition;
      import com.example.springbootday02.condition.MyConditionOnClass;
      import com.example.springbootday02.domain.User;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Conditional;
      import org.springframework.context.annotation.Configuration;
      
      @Configuration
      public class UserConfig {
      
          @Bean("user")
      //    @Conditional(ClassCondition.class)
          @MyConditionOnClass("redis.clients.jedis.Jedis")
          public User getUser(){
              return new User();
          }
      }
      
  使用springboot提供的注解 实现条件判断
   @Bean("user2")
      @ConditionalOnProperty(name = "itoldlu",havingValue = "oldlu")
      public User getUser2(){
          return new User();
      }
      
   itoldlu=oldlu

1.2 Condition 小结

  1. 自定义条件:
    1. 定义条件类:自定义类实现Condition接口,重写 matches 方法,在 matches 方法中进行逻辑判断,返回 boolean值 。 matches 方法两个参数:
      1. context:上下文对象,可以获取属性值,获取类加载器,获取BeanFactory等。
      2. metadata:元数据对象,用于获取注解属性。
    2. 判断条件: 在初始化Bean时,使用 @Conditional(条件类.class)注解
  2. SpringBoot 提供的常用条件注解:
    img
    1. ConditionalOnProperty:判断配置文件中是否有对应属性和值才初始化Bean
    2. ConditionalOnClass:判断环境中是否有对应字节码文件才初始化Bean
    3. ConditionalOnMissingBean:判断环境中没有对应Bean才初始化Bean

1.3 切换内置web服务器

SpringBoot的web环境中默认使用tomcat作为内置服务器,其实SpringBoot提供了4中内置服务器供我们选择,我们可 以很方便的进行切换。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

1.4 @Enable*注解

SpringBoot中提供了很多Enable开头的注解,这些注解都是用于动态启用某些功能的。而其底层原理是使用@Import注 解导入一些配置类,实现Bean的动态加载。

思考:SpringBoot 工程是否可以直接获取jar包中定义的Bean?

package com.example.springbootday02enable;

import com.example.springbootday02enableother.config.EnableUser;
import com.example.springbootday02enableother.config.UserConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

/**
 * @ComponentScan 扫描范围:当前引导类所在包及其子包
 *
 * com.itoldlu.springbootenable
 * com.itoldlu.config
 * //1.使用@ComponentScan扫描com.itoldlu.config包
 * //2.可以使用@Import注解,加载类。这些类都会被Spring创建,并放入IOC容器
 * //3.可以对Import注解进行封装。
 */
@SpringBootApplication
//@ComponentScan(basePackages="com.example.springbootday02enableother")
//@Import(UserConfig.class)
@EnableUser
public class SpringbootDay02EnableApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SpringbootDay02EnableApplication.class, args);
        Object user = run.getBean("user");
        System.out.println(user);
    }

}

package com.example.springbootday02enableother.config;

import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(UserConfig.class)
public @interface EnableUser {
}

package com.example.springbootday02enableother.config;

import com.example.springbootday02enableother.domain.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class UserConfig {

    @Bean("user")
    public User getUser(){
        return new User();
    }
}

package com.example.springbootday02enableother.domain;

public class User {
}

1.5 @Import注解

@Enable*底层依赖于@Import注解导入一些类,使用@Import导入的类会被Spring加载到IOC容器中。而@Import提供4中用 法:

  1. 导入Bean
  2. 导入配置类
  3. 导入 ImportSelector 实现类。一般用于加载配置文件中的类
  4. 导入 ImportBeanDefinitionRegistrar 实现类。
package com.example.springbootday02enable;

import com.example.springbootday02enableother.config.EnableUser;
import com.example.springbootday02enableother.config.MyImportBeanDefinitionRegistrar;
import com.example.springbootday02enableother.config.MyImportSelector;
import com.example.springbootday02enableother.config.UserConfig;
import com.example.springbootday02enableother.domain.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

/**
 * @ComponentScan 扫描范围:当前引导类所在包及其子包
 *
 * com.itoldlu.springbootenable
 * com.itoldlu.config
 * //1.使用@ComponentScan扫描com.itoldlu.config包
 * //2.可以使用@Import注解,加载类。这些类都会被Spring创建,并放入IOC容器
 * //3.可以对Import注解进行封装。
 */
/**
 * Import4中用法:
 *  1. 导入Bean
 *  2. 导入配置类
 *  3. 导入ImportSelector的实现类。
 *  4. 导入ImportBeanDefinitionRegistrar实现类
 */

@SpringBootApplication
//@ComponentScan(basePackages="com.example.springbootday02enableother")
//@Import(UserConfig.class)
//@EnableUser

//@Import(User.class)
//@Import(UserConfig.class)
//@Import(MyImportSelector.class)
@Import(MyImportBeanDefinitionRegistrar.class)
public class SpringbootDay02EnableApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SpringbootDay02EnableApplication.class, args);
        Object user = run.getBean(User.class);
        System.out.println(user);
        Object user1 = run.getBean("user");
        System.out.println(user1);
    }

}

package com.example.springbootday02enableother.config;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[]{"com.example.springbootday02enableother.domain.User"};
    }
}

    package com.example.springbootday02enableother.config;

    import com.example.springbootday02enableother.domain.User;
    import org.springframework.beans.factory.support.AbstractBeanDefinition;
    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
    import org.springframework.beans.factory.support.BeanDefinitionRegistry;
    import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
    import org.springframework.core.type.AnnotationMetadata;

    public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

        @Override
        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
            AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition();
            registry.registerBeanDefinition("user",beanDefinition);
        }
    }

1.6 面试题:@EnableAutoConfiguration注解

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  1. @EnableAutoConfiguration 注解内部使用 @Import(AutoConfigurationImportSelector.class)来加载配置类。(AutoConfigurationImportSelector实现类的目的就是扫描下面的配置文件)
  2. 配置文件位置:META-INF/spring.factories,该配置文件中定义了大量的配置类,当 SpringBoot 应用启动时,会自动加载 这些配置类,初始化Bean
  3. 并不是所有的Bean都会被初始化,在配置类中使用Condition来加载满足条件的Bean

1.6 案例:需求

自定义redis-starter。要求当导入redis坐标时,SpringBoot自动创建Jedis的Bean

案例:实现步骤

  1. 创建 redis-spring-boot-autoconfigure 模块

    1. 导入依赖
      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>2.3.3.RELEASE</version>
              <relativePath/> <!-- lookup parent from repository -->
          </parent>
          <groupId>com.example</groupId>
          <artifactId>redis-spring-boot-autoconfigure</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <name>redis-spring-boot-autoconfigure</name>
          <description>Demo project for Spring Boot</description>
      
          <properties>
              <java.version>1.8</java.version>
          </properties>
      
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter</artifactId>
              </dependency>
              <!--引入jedis依赖-->
              <dependency>
                  <groupId>redis.clients</groupId>
                  <artifactId>jedis</artifactId>
              </dependency>
          </dependencies>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
      
      </project>
  1. 创建 redis-spring-boot-starter 模块,依赖 redis-springboot-autoconfigure的模块

    1. 导入依赖
       <dependency>
                  <groupId>com.example</groupId>
                  <artifactId>redis-spring-boot-autoconfigure</artifactId>
                  <version>0.0.1-SNAPSHOT</version>
              </dependency>
  1. 在 redis-spring-boot-autoconfigure 模块中初始化 Jedis 的 Bean。并定义META-INF/spring.factories 文件
   package com.example.redisspringbootautoconfigure.config;
   
   import org.springframework.boot.context.properties.EnableConfigurationProperties;
   import org.springframework.context.annotation.Bean;
   import org.springframework.context.annotation.Configuration;
   import redis.clients.jedis.Jedis;
   
   @Configuration
   @EnableConfigurationProperties(RedisProperties.class)
   public class RedisAutoConfiguration {
   
       @Bean
       public Jedis jedis(RedisProperties redisProperties){
           return new Jedis(redisProperties.getHost(),redisProperties.getPort());
       }
   }
   
   package com.example.redisspringbootautoconfigure.config;
   
   import org.springframework.boot.context.properties.ConfigurationProperties;
   import org.springframework.stereotype.Component;
   
   @ConfigurationProperties(prefix = "redis")
   public class RedisProperties {
   
       private String host = "localhost";
       private int port = 6379;
   
       public String getHost() {
           return host;
       }
   
       public void setHost(String host) {
           this.host = host;
       }
   
       public int getPort() {
           return port;
       }
   
       public void setPort(int port) {
           this.port = port;
       }
   }
   
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.redisspringbootautoconfigure.config.RedisAutoConfiguration

META-INF / spring.properties

  1. 在测试模块中引入自定义的 redis-starter 依赖,测试获取 Jedis 的Bean,操作 redis。
   package com.example.springbootday02enable;
   
   import com.example.springbootday02enableother.config.EnableUser;
   import com.example.springbootday02enableother.config.MyImportBeanDefinitionRegistrar;
   import com.example.springbootday02enableother.config.MyImportSelector;
   import com.example.springbootday02enableother.config.UserConfig;
   import com.example.springbootday02enableother.domain.User;
   import org.springframework.boot.SpringApplication;
   import org.springframework.boot.autoconfigure.SpringBootApplication;
   import org.springframework.context.ConfigurableApplicationContext;
   import org.springframework.context.annotation.Bean;
   import org.springframework.context.annotation.ComponentScan;
   import org.springframework.context.annotation.Import;
   import org.springframework.stereotype.Component;
   import redis.clients.jedis.Jedis;
   
  /**
    * @ComponentScan 扫描范围:当前引导类所在包及其子包
    *
​```java
com.itoldlu.springbootenable
    com.itoldlu.config
     //1.使用@ComponentScan扫描com.itoldlu.config包
     //2.可以使用@Import注解,加载类。这些类都会被Spring创建,并放入IOC容器
     //3.可以对Import注解进行封装。
  Import4中用法:
     1. 导入Bean
     2. 导入配置类
    3. 导入ImportSelector的实现类。
     4. 导入ImportBeanDefinitionRegistrar实现类
 
   @SpringBootApplication
   //@ComponentScan(basePackages="com.example.springbootday02enableother")
   //@Import(UserConfig.class)
   //@EnableUser
   
   //@Import(User.class)
   //@Import(UserConfig.class)
   //@Import(MyImportSelector.class)
   //@Import(MyImportBeanDefinitionRegistrar.class)
   public class SpringbootDay02EnableApplication {
   
       public static void main(String[] args) {
           ConfigurableApplicationContext run = SpringApplication.run(SpringbootDay02EnableApplication.class, args);
   //        Object user = run.getBean(User.class);
   //        System.out.println(user);
   //        Object user1 = run.getBean("user");
   //        System.out.println(user1);
   
           Jedis bean = run.getBean(Jedis.class);
           System.out.println(bean);
           bean.set("test","oldlu");
           System.out.println(bean.get("test"));
       
       }
       
       @Bean
       public Jedis jedis(){
           return new Jedis("localhost",6379);
       }
   }
  package com.example.redisspringbootautoconfigure.config;
   
   import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
   import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
   import org.springframework.boot.context.properties.EnableConfigurationProperties;
   import org.springframework.context.annotation.Bean;
   import org.springframework.context.annotation.Configuration;
   import redis.clients.jedis.Jedis;
   
   @Configuration
   @EnableConfigurationProperties(RedisProperties.class)
   @ConditionalOnClass(Jedis.class)
   public class RedisAutoConfiguration {
   
       @Bean
       @ConditionalOnMissingBean(Jedis.class)
       public Jedis jedis(RedisProperties redisProperties){
           System.out.println("RedisAutoConfiguration....");
           return new Jedis(redisProperties.getHost(),redisProperties.getPort());
       }
   }

2 自定义start

SpringBoot核心是maven依赖管理和start,我们自定义的start可以引入第三方依赖,并且提供自定义bean对象交给Spring容器管理。其他项目直接引入自定义start,就可以省去引入第三方依赖和手动创建bean的繁琐操作,其实这里每个第三方公司想要更好的兼容springboot,一般每导入一个第三方的依赖,除了本身的jar包以外,还会有一个 xxx-spring-boot-autoConfigure,这个就是第三方依赖自己编写的自动配置类,然后都会通过注解被自动扫描后注入bean,自定义start没什么实际意义只是加以区别即可!

2.1 如何区分自定义start和官方start

官方start的命名规范是spring-boot-starter-**比如下面的这个web依赖,而自定义start名字一般是**-spring-boot-start

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.2 自定义start的原理

SpringBoot在启动的时候会扫描所有依赖或者说jar包中resources/META-INF/spring.factories文件,并会将该文件中配置的类注入到IOC容器中

步骤

1.在自定义的start项目中声明一个配置类,在该配置类中配置一个bean

2.将该配置类通过pringframework.boot.autoconfigure.EnableAutoConfiguration配置到spring.factories文件中即可

2.3 自定义start功能描述及实现

需求

现在需要自定义个start,功能是提供一个Person对象。在其他Springboot模块中引入该start,并且获取该bean对象

1.创建一个配置文件

package com.example.config;

import com.example.Cat;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CatAutoConfiguration {

    @Bean
    public Cat cat(){
        return new Cat("tom",12);
    }
}

2.添加resources/META-INF/spring.factories配置

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.config.CatAutoConfiguration

2.4 代码升级

升级原因

cat()方法中返回的对象的属性是写死的,最好改成可配置的

升级步骤

1.在Cat类上添加@ConfigurationProperties(prefix = “cat”)注解,引用配置文件中的数据,代码如下

import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Objects;

@ConfigurationProperties(prefix = "cat")
public class Cat {
    private String name;
    private Integer age;
	//..get/set..
}

2.在配置类中使用EnableConfigurationProperties引入Cat配置文件,并将cat注入到cat方法中代码如下

@Configuration
@EnableConfigurationProperties(Cat.class)
public class CatAutoConfiguration {

    @Bean
    public Cat cat(Cat cat){
        return new Cat(cat.getName(),cat.getAge());
    }
}

注意事项:此种方式只有配置文件在SpringBoot项目才生效,在当前start项目的配置文件中不生效

2.5 注意事项

start项目的pom中不能有build标签,否则引入该start的springboot项目不能正常打包

2.6 总结

SpringBoot启动时会自动搜索包含spring.factories文件的JAR包;
根据spring.factories文件加载自动配置类AutoConfiguration;
通过AutoConfiguration类,加载满足条件(@ConditionalOnXxx)的bean到Spring IOC容器中;
使用者可以直接使用自动加载到IOC的bean。
小伙伴们是否想起曾经被 SSM 整合支配的恐惧?相信很多小伙伴都是有过这样的经历的,一大堆配置问题,各种排除扫描,导入一个新的依赖又得添加新的配置。自从有了 SpringBoot 之后,咋们就起飞了!各种零配置开箱即用,而我们之所以开发起来能够这么爽,自动配置的功劳少不了,今天我们就一起来讨论一下 SpringBoot 自动配置原理。

3 SpringBoot 源码常用注解拾遗

这部分主要讲一下 SpringBoot 源码中经常使用到的注解,以扫清后面阅读源码时候的障碍。

3.1 组合注解

当可能大量同时使用到几个注解到同一个类上,就可以考虑将这几个注解到别的注解上。被注解的注解我们就称之为组合注解。

  • 元注解:可以注解到别的注解上的注解。
  • 组合注解:被注解的注解我们就称之为组合注解。

3.2 @Value 【Spring 提供】

@Value 就相当于传统 xml 配置文件中的 value 字段。

假设存在代码:

@Component 
public class Person { 
@Value("i am name")
private String name;
} 

上面代码等价于的配置文件:

<bean class="Person"> 
<property name ="name" value="i am name"></property>
</bean> 

我们知道配置文件中的 value 的取值可以是:

  • 字面量
  • 通过 ${key} 方式从环境变量中获取值
  • 通过 ${key} 方式全局配置文件中获取值
  • #{SpEL}

所以,我们就可以通过 @Value(${key}) 的方式获取全局配置文件中的指定配置项。

3.3 @ConfigurationProperties 【SpringBoot 提供】

如果我们需要取 N 个配置项,通过 @Value 的方式去配置项需要一个一个去取,这就显得有点 low 了。我们可以使用 @ConfigurationProperties

标有 @ConfigurationProperties 的类的所有属性和配置文件中相关的配置项进行绑定。(默认从全局配置文件中获取配置值),绑定之后我们就可以通过这个类去访问全局配置文件中的属性值了。

下面看一个实例:

1.在主配置文件中添加如下配置

person.name=kundy 
person.age=13 
person.sex=male 

2.创建配置类,由于篇幅问题这里省略了 setter、getter 方法,但是实际开发中这个是必须的,否则无法成功注入。另外,@Component 这个注解也还是需要添加的。

@Component 
@ConfigurationProperties(prefix = "person") 
public class Person { 
private String name; 
private Integer age; 
private String sex; 
} 

这里 @ConfigurationProperties 有一个 prefix 参数,主要是用来指定该配置项在配置文件中的前缀。

3.测试,在 SpringBoot 环境中,编写个测试方法,注入 Person 类,即可通过 Person 对象取到配置文件的值。

3.4 @Import 【Spring 提供】

@Import 注解支持导入普通 java 类,并将其声明成一个bean。主要用于将多个分散的 java config 配置类融合成一个更大的 config 类。

  • @Import 注解在 4.2 之前只支持导入配置类。
  • 在4.2之后 @Import 注解支持导入普通的 java 类,并将其声明成一个 bean。

@Import 三种使用方式

  • 直接导入普通的 Java 类。
  • 配合自定义的 ImportSelector 使用。
  • 配合 ImportBeanDefinitionRegistrar 使用。

1. 直接导入普通的 Java 类

1.创建一个普通的 Java 类。

public class Circle { 

public void sayHi() { 
System.out.println("Circle sayHi()"); 
} 

} 

2.创建一个配置类,里面没有显式声明任何的 Bean,然后将刚才创建的 Circle 导入。

@Import({Circle.class}) 
@Configuration 
public class MainConfig { 

} 

3.创建测试类。

public static void main(String[] args) { 

ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class); 
Circle circle = context.getBean(Circle.class); 
circle.sayHi(); 

}

4.运行结果:

Circle sayHi()

可以看到我们顺利的从 IOC 容器中获取到了 Circle 对象,证明我们在配置类中导入的 Circle 类,确实被声明为了一个 Bean。

2. 配合自定义的 ImportSelector 使用

ImportSelector 是一个接口,该接口中只有一个 selectImports 方法,用于返回全类名数组。所以利用该特性我们可以给容器动态导入 N 个 Bean。

1.创建普通 Java 类 Triangle。

public static void main(String[] args) { 

ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class); 
Circle circle = context.getBean(Circle.class); 
circle.sayHi(); 

}

2.创建 ImportSelector 实现类,selectImports 返回 Triangle 的全类名。

public class MyImportSelector implements ImportSelector { 

@Override 
public String[] selectImports(AnnotationMetadata annotationMetadata) { 
return new String[]{"annotation.importannotation.waytwo.Triangle"}; 
} 

}

3.创建配置类,在原来的基础上还导入了 MyImportSelector。

@Import({Circle.class,MyImportSelector.class}) 
@Configuration 
public class MainConfigTwo { 

} 

4.创建测试类

public static void main(String[] args) { 

ApplicationContext context = new AnnotationConfigApplicationContext(MainConfigTwo.class); 
Circle circle = context.getBean(Circle.class); 
Triangle triangle = context.getBean(Triangle.class); 
circle.sayHi(); 
triangle.sayHi(); 

} 

5.运行结果:

Circle sayHi()
Triangle sayHi()

可以看到 Triangle 对象也被 IOC 容器成功的实例化出来了。

3. 配合 ImportBeanDefinitionRegistrar 使用

ImportBeanDefinitionRegistrar 也是一个接口,它可以手动注册bean到容器中,从而我们可以对类进行个性化的定制。(需要搭配 @Import 与 @Configuration 一起使用。)

1.创建普通 Java 类 Rectangle。

public class Rectangle { 

public void sayHi() { 
System.out.println("Rectangle sayHi()"); 
} 

}

2.创建 ImportBeanDefinitionRegistrar 实现类,实现方法直接手动注册一个名叫 rectangle 的 Bean 到 IOC 容器中。

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { 

@Override 
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) { 

RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Rectangle.class); 
// 注册一个名字叫做 rectangle 的 bean 
beanDefinitionRegistry.registerBeanDefinition("rectangle", rootBeanDefinition); 
} 

}

3.创建配置类,导入 MyImportBeanDefinitionRegistrar 类。

@Import({Circle.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class}) 
@Configuration 
public class MainConfigThree { 

}

4.创建测试类。

public static void main(String[] args) { 

ApplicationContext context = new AnnotationConfigApplicationContext(MainConfigThree.class); 
Circle circle = context.getBean(Circle.class); 
Triangle triangle = context.getBean(Triangle.class); 
Rectangle rectangle = context.getBean(Rectangle.class); 
circle.sayHi(); 
triangle.sayHi(); 
rectangle.sayHi(); 

}

5.运行结果

Circle sayHi()
Triangle sayHi()
Rectangle sayHi()

嗯对,Rectangle 对象也被注册进来了。


4 SpringBoot 启动过程

在这里插入图片描述

在看源码的过程中,我们会看到以下四个类的方法经常会被调用,我们需要对一下几个类有点印象:

  • ApplicationContextInitializer
  • ApplicationRunner
  • CommandLineRunner
  • SpringApplicationRunListener

下面开始源码分析,先从 SpringBoot 的启动类的 run() 方法开始看,以下是调用链:SpringApplication.run() -> run(new Class[]{primarySource}, args) -> new SpringApplication(primarySources)).run(args)

一直在run,终于到重点了,我们直接看 new SpringApplication(primarySources)).run(args) 这个方法。

上面的方法主要包括两大步骤:

  • 创建 SpringApplication 对象。
  • 运行 run() 方法。

4.1 创建 SpringApplication 对象

public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) { 
 
this.sources = new LinkedHashSet(); 
this.bannerMode = Mode.CONSOLE; 
this.logStartupInfo = true; 
this.addCommandLineProperties = true; 
this.addConversionService = true; 
this.headless = true; 
this.registerShutdownHook = true; 
this.additionalProfiles = new HashSet(); 
this.isCustomEnvironment = false; 
this.resourceLoader = resourceLoader; 
Assert.notNull(primarySources, "PrimarySources must not be null"); 
// 保存主配置类(这里是一个数组,说明可以有多个主配置类) 
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources)); 
// 判断当前是否是一个 Web 应用 
this.webApplicationType = WebApplicationType.deduceFromClasspath(); 
// 从类路径下找到 META/INF/Spring.factories 配置的所有 ApplicationContextInitializer,然后保存起来 
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class)); 
// 从类路径下找到 META/INF/Spring.factories 配置的所有 ApplicationListener,然后保存起来 
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class)); 
// 从多个配置类中找到有 main 方法的主配置类(只有一个) 
this.mainApplicationClass = this.deduceMainApplicationClass(); 
 
}

4.2 运行run()方法


public ConfigurableApplicationContext run(String... args) { 
 
// 创建计时器 
StopWatch stopWatch = new StopWatch(); 
stopWatch.start(); 
// 声明 IOC 容器 
ConfigurableApplicationContext context = null; 
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList(); 
this.configureHeadlessProperty(); 
// 从类路径下找到 META/INF/Spring.factories 获取 SpringApplicationRunListeners 
SpringApplicationRunListeners listeners = this.getRunListeners(args); 
// 回调所有 SpringApplicationRunListeners 的 starting() 方法 
listeners.starting(); 
Collection exceptionReporters; 
try { 
// 封装命令行参数 
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); 
// 准备环境,包括创建环境,创建环境完成后回调 SpringApplicationRunListeners#environmentPrepared()方法,表示环境准备完成 
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments); 
this.configureIgnoreBeanInfo(environment); 
// 打印 Banner 
Banner printedBanner = this.printBanner(environment); 
// 创建 IOC 容器(决定创建 web 的 IOC 容器还是普通的 IOC 容器) 
context = this.createApplicationContext(); 
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context); 
/*
 * 准备上下文环境,将 environment 保存到 IOC 容器中,并且调用 applyInitializers() 方法
 * applyInitializers() 方法回调之前保存的所有的 ApplicationContextInitializer 的 initialize() 方法
 * 然后回调所有的 SpringApplicationRunListener#contextPrepared() 方法 
 * 最后回调所有的 SpringApplicationRunListener#contextLoaded() 方法 
 */
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner); 
// 刷新容器,IOC 容器初始化(如果是 Web 应用还会创建嵌入式的 Tomcat),扫描、创建、加载所有组件的地方 
this.refreshContext(context); 
// 从 IOC 容器中获取所有的 ApplicationRunner 和 CommandLineRunner 进行回调 
this.afterRefresh(context, applicationArguments); 
stopWatch.stop(); 
if (this.logStartupInfo) { 
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch); 
} 
// 调用 所有 SpringApplicationRunListeners#started()方法 
listeners.started(context); 
this.callRunners(context, applicationArguments); 
} catch (Throwable var10) { 
this.handleRunFailure(context, var10, exceptionReporters, listeners); 
throw new IllegalStateException(var10); 
} 
try { 
listeners.running(context); 
return context; 
} catch (Throwable var9) { 
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null); 
throw new IllegalStateException(var9); 
} 
}

4.3 小结

run() 阶段主要就是回调本节开头提到过的4个监听器中的方法与加载项目中组件到 IOC 容器中,而所有需要回调的监听器都是从类路径下的 META/INF/Spring.factories 中获取,从而达到启动前后的各种定制操作。


5 SpringBoot自动配置原理

5.1 @SpringBootApplication 注解

SpringBoot 项目的一切都要从 @SpringBootApplication 这个注解开始说起。

@SpringBootApplication 标注在某个类上说明:

  • 这个类是 SpringBoot 的主配置类。
  • SpringBoot 就应该运行这个类的 main 方法来启动 SpringBoot 应用。

该注解的定义如下:

@SpringBootConfiguration 
@EnableAutoConfiguration 
@ComponentScan( 
excludeFilters = {@Filter( 
type = FilterType.CUSTOM, 
classes = {TypeExcludeFilter.class} 
), @Filter( 
type = FilterType.CUSTOM, 
classes = {AutoConfigurationExcludeFilter.class} 
)} 
) 
public @interface SpringBootApplication { 
 
}

可以看到 SpringBootApplication 注解是一个组合注解(关于组合注解文章的开头有讲到),其主要组合了一下三个注解:

  • @SpringBootConfiguration:该注解表示这是一个 SpringBoot 的配置类,其实它就是一个 @Configuration 注解而已。
  • @ComponentScan:开启组件扫描。
  • @EnableAutoConfiguration:从名字就可以看出来,就是这个类开启自动配置的。嗯,自动配置的奥秘全都在这个注解里面。

5.2 @EnableAutoConfiguration 注解

先看该注解是怎么定义的:

@AutoConfigurationPackage 
@Import({AutoConfigurationImportSelector.class}) 
public @interface EnableAutoConfiguration { }

@AutoConfigurationPackage

从字面意思理解就是自动配置包。点进去可以看到就是一个 @Import 注解:@Import({Registrar.class}),导入了一个 Registrar 的组件。关于 @Import 的用法文章上面也有介绍哦。

我们在 Registrar 类中的 registerBeanDefinitions 方法上打上断点,可以看到返回了一个包名,该包名其实就是主配置类所在的包。
在这里插入图片描述

一句话:@AutoConfigurationPackage 注解就是将主配置类(@SpringBootConfiguration标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器中。所以说,默认情况下主配置类包及子包以外的组件,Spring 容器是扫描不到的。

5.3 @Import({AutoConfigurationImportSelector.class})

该注解给当前配置类导入另外的 N 个自动配置类。(该注解详细用法上文有提及)。

配置类导入规则

那具体的导入规则是什么呢?我们来看一下源码。在开始看源码之前,先啰嗦两句。就像小马哥说的,我们看源码不用全部都看,不用每一行代码都弄明白是什么意思,我们只要抓住关键的地方就可以了。

我们知道 AutoConfigurationImportSelector 的 selectImports 就是用来返回需要导入的组件的全类名数组的,那么如何得到这些数组呢?

在 selectImports 方法中调用了一个 getAutoConfigurationEntry() 方法。
在这里插入图片描述

由于篇幅问题我就不一一截图了,我直接告诉你们调用链:在 getAutoConfigurationEntry() -> getCandidateConfigurations() -> loadFactoryNames()

在这里 loadFactoryNames() 方法传入了 EnableAutoConfiguration.class 这个参数。先记住这个参数,等下会用到。

在这里插入图片描述

loadFactoryNames() 中关键的三步:

  • 从当前项目的类路径中获取所有 META-INF/spring.factories 这个文件下的信息。
  • 将上面获取到的信息封装成一个 Map 返回。
  • 从返回的 Map 中通过刚才传入的 EnableAutoConfiguration.class 参数,获取该 key 下的所有值。

**在这里插入图片描述
**

META-INF/spring.factories 探究

听我这样说完可能会有点懵,我们来看一下 META-INF/spring.factories 这类文件是什么就不懵了。当然在很多第三方依赖中都会有这个文件,一般每导入一个第三方的依赖,除了本身的jar包以外,还会有一个 xxx-spring-boot-autoConfigure,这个就是第三方依赖自己编写的自动配置类。我们现在就以 spring-boot-autocongigure 这个依赖来说。
在这里插入图片描述

可以看到 EnableAutoConfiguration 下面有很多类,这些就是我们项目进行自动配置的类。

一句话:将类路径下 META-INF/spring.factories 里面配置的所有 EnableAutoConfiguration 的值加入到 Spring 容器中。

HttpEncodingAutoConfiguration

通过上面方式,所有的自动配置类就被导进主配置类中了。但是这么多的配置类,明显有很多自动配置我们平常是没有使用到的,没理由全部都生效吧。

接下来我们以 HttpEncodingAutoConfiguration为例来看一个自动配置类是怎么工作的。为啥选这个类呢?主要是这个类比较的简单典型。

先看一下该类标有的注解:

@Configuration 
@EnableConfigurationProperties({HttpProperties.class}) 
@ConditionalOnWebApplication( 
type = Type.SERVLET 
) 
@ConditionalOnClass({CharacterEncodingFilter.class}) 
@ConditionalOnProperty( 
prefix = "spring.http.encoding", 
value = {"enabled"}, 
matchIfMissing = true 
) 
public class HttpEncodingAutoConfiguration { 
 
}
  • @Configuration:**标记为配置类。
  • @ConditionalOnWebApplication:**web应用下才生效。
  • @ConditionalOnClass:**指定的类(依赖)存在才生效。
  • @ConditionalOnProperty:**主配置文件中存在指定的属性才生效。
  • @EnableConfigurationProperties({HttpProperties.class}):**启动指定类的ConfigurationProperties功能;将配置文件中对应的值和 HttpProperties 绑定起来;并把 HttpProperties 加入到 IOC 容器中。

因为 @EnableConfigurationProperties({HttpProperties.class})把配置文件中的配置项与当前 HttpProperties 类绑定上了。

然后在 HttpEncodingAutoConfiguration 中又引用了 HttpProperties ,所以最后就能在 HttpEncodingAutoConfiguration 中使用配置文件中的值了。

最终通过 @Bean 和一些条件判断往容器中添加组件,实现自动配置。(当然该Bean中属性值是从 HttpProperties 中获取)

HttpProperties

HttpProperties 通过 @ConfigurationProperties 注解将配置文件与自身属性绑定。

所有在配置文件中能配置的属性都是在 xxxProperties 类中封装着;配置文件能配置什么就可以参照某个功能对应的这个属性类。

@ConfigurationProperties( 
prefix = "spring.http" 
)// 从配置文件中获取指定的值和bean的属性进行绑定 
public class HttpProperties { 
}

5.4 小结

  • SpringBoot启动会加载大量的自动配置类。
  • 我们看需要的功能有没有SpringBoot默认写好的自动配置类。
  • 我们再来看这个自动配置类中到底配置了那些组件(只要我们要用的组件有,我们就不需要再来配置了)。
  • 给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们就可以在配置文件中指定这些属性的值。
    xxxAutoConfiguration:自动配置类给容器中添加组件。
    xxxProperties:封装配置文件中相关属性。

不知道小伙伴们有没有发现,很多需要待加载的类都放在类路径下的META-INF/Spring.factories 文件下,而不是直接写死这代码中,这样做就可以很方便我们自己或者是第三方去扩展,我们也可以实现自己 starter,让SpringBoot 去加载。现在明白为什么 SpringBoot 可以实现零配置,开箱即用了吧!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵广陆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值