好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot借助spring.factories文件跨模块实例化Bean

1. 前言

SpringBoot在包扫描时,并不会扫描子模块下的内容,这样就使得我们的子模块中的Bean无法注入到Spring容器中。 SpringBoot 就为我们提供了 spring.factories 这个文件,让我们可以轻松的将子模块的Bean注入到我们的Spring容器中,本篇文章我们就一起探究一下 spring.factories 跨模块实例化Bean的原理。

我们在 SpringBoot项目为何引入大量的starter?如何自定义starter? 文章中也讲到构建自己构建starter,其中 spring.factories 就起到重要的作用,我们是通过 spring.factories 让starer项目中的Bean注入到Web模块的Spring容器中。本篇文章就来探究一下spring.factories文件,更深层次的东西,以及我们是如何借助该文件实例化Bean的。

2. 配置

spring.factories 文件一般都是配置在 src/main/resources/META-INF/  目录下。

也就是说我们在IDEA新建的SpringBoot项目或者Maven项目的资源文件resources目录下新建一个META-INF文件夹,再建一个spring.factories文件即可,新建的文件没有问题的化,一般IDEA都能自动识别,如下图所示。

spring.factories 的文件内容就是接口对应其实现类,实现类可以有多个

文件内容必须是kv形式,即properties类型

?

1

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zhj.config.AutoConfiguration

如其一个接口有多个实现,如下配置:

?

1

2

3

4

org.springframework.boot.logging.LoggingSystemFactory=\

org.springframework.boot.logging.logback.LogbackLoggingSystem.Factory,\

org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.Factory,\

org.springframework.boot.logging.java.JavaLoggingSystem.Factory

3. 原理

在 spring -core  中定义了 SpringFactoriesLoader 类 ,这个类就是让spring.factories文件发挥作用的类。SpringFactoriesLoader类的作用就是检索META-INF/spring.factories文件,并获取指定接口将其实现实例化。 在这个类中定义了两个对外的方法:

loadFactories  根据给定的接口类获取其实现类的实例,这个方法返回的是对象列表 loadFactoryNames  根据给定的类型加载类路径的全限定类名,这个方法返回的是全限定类名的列表。

源码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

public final class SpringFactoriesLoader {

    

     // 文件位置

     public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories" ;

    

     // 缓存

     private static final Map<ClassLoader, MultiValueMap<String, String>> cache = new ConcurrentReferenceHashMap<>();

?

?

     private SpringFactoriesLoader() {

     }

?

?

     /**

      * 根据给定的类型加载并实例化工厂的实现类

      */

     public static <T> List<T> loadFactories(Class<T> factoryType, @Nullable ClassLoader classLoader) {

         Assert.notNull(factoryType, "'factoryType' must not be null" );

         // 获取类加载器

         ClassLoader classLoaderToUse = classLoader;

         if (classLoaderToUse == null ) {

             classLoaderToUse = SpringFactoriesLoader. class .getClassLoader();

         }

         // 加载类的全限定名

         List<String> factoryImplementationNames = loadFactoryNames(factoryType, classLoaderToUse);

         if (logger.isTraceEnabled()) {

             logger.trace( "Loaded [" + factoryType.getName() + "] names: " + factoryImplementationNames);

         }

         List<T> result = new ArrayList<>(factoryImplementationNames.size());

         for (String factoryImplementationName : factoryImplementationNames) {

             // 实例化Bean,并将Bean放入到List集合中

             result.add(instantiateFactory(factoryImplementationName, factoryType, classLoaderToUse));

         }

         AnnotationAwareOrderComparator.sort(result);

         return result;

     }

    

     /**

      * 根据给定的类型加载类路径的全限定类名

      */

     public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {

        

         // 获取工厂类型名称

         String factoryTypeName = factoryType.getName();

         // 加载所有META-INF/spring.factories中的value

         return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());

     }

    

     private static Map<String, List<String>> loadSpringFactories( @Nullable ClassLoader classLoader) {

         // 根据类加载器从缓存中获取,如果缓存中存在,就直接返回,如果不存在就去加载

         MultiValueMap<String, String> result = cache.get(classLoader);

         if (result != null ) {

             return result;

         }

    

         try {

             // 获取所有jar中classpath路径下的META-INF/spring.factories

             Enumeration<URL> urls = (classLoader != null ?

                     classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :

                     ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));

             result = new LinkedMultiValueMap<>();

             // 遍历所有的META-INF/spring.factories

             while (urls.hasMoreElements()) {

                 URL url = urls.nextElement();

                 UrlResource resource = new UrlResource(url);

                 // 将META-INF/spring.factories中的key value加载为Prpperties对象

                 Properties properties = PropertiesLoaderUtils.loadProperties(resource);

                 for (Map.Entry<?, ?> entry : properties.entrySet()) {

                     // key就是接口的类名称

                     String factoryTypeName = ((String) entry.getKey()).trim();

                     for (String factoryImplementationName : StringUtils测试数据maDelimitedListToStringArray((String) entry.getValue())) {

                         // 以factoryTypeName为key,实现类为value放入map集合中

                         result.add(factoryTypeName, factoryImplementationName.trim());

                     }

                 }

             }

             // 加入缓存

             cache.put(classLoader, result);

             return result;

         }

         catch (IOException ex) {

             throw new IllegalArgumentException( "Unable to load factories from location [" +

                     FACTORIES_RESOURCE_LOCATION + "]" , ex);

         }

     }

    

     // 通过反射实例化Bean对象

     @SuppressWarnings ( "unchecked" )

     private static <T> T instantiateFactory(String factoryImplementationName, Class<T> factoryType, ClassLoader classLoader) {

         try {

             Class<?> factoryImplementationClass = ClassUtils.forName(factoryImplementationName, classLoader);

             if (!factoryType.isAssignableFrom(factoryImplementationClass)) {

                 throw new IllegalArgumentException(

                         "Class [" + factoryImplementationName + "] is not assignable to factory type [" + factoryType.getName() + "]" );

             }

             return (T) ReflectionUtils.accessibleConstructor(factoryImplementationClass).newInstance();

         }

         catch (Throwable ex) {

             throw new IllegalArgumentException(

                 "Unable to instantiate factory class [" + factoryImplementationName + "] for factory type [" + factoryType.getName() + "]" ,

                 ex);

         }

     }

?

}

4. 总结

Spring通过 SpringFactoriesLoader 实例化Bean的过程

获取 SpringFactoriesLoader 对应的类加载器 查找缓存,查看缓存中是否已经读取到所有jar中classpath路径下的META-INF/spring.factories的内容 如果缓存已经存在,根据/spring.factories文件中配置的全限定类名通过反射实例化Bean 如果缓存中没有值,则扫描所有jar中的这个META-INF/spring.factories文件,并将其以读取到缓存中,并返回这个配置列表 然后根据这个全限定类名的列表再通过反射实例化Bean

到此这篇关于SpringBoot借助spring.factories文件跨模块实例化Bean的文章就介绍到这了,更多相关SpringBoot实例化Bean内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://juejin.cn/post/7029683137865056292

查看更多关于SpringBoot借助spring.factories文件跨模块实例化Bean的详细内容...

  阅读:26次