上海包装设计公司排行郑州网站seo公司
initializers成员变量定义如下:
在构造方法里通过setInitializers
setInitializers的代码很简单:
其中的参数通过getSpringFactoriesInstances来获取的,该方法的代码如下:
其中调用的重载的方法代码如下:
它调用了SpringFactoriesLoader的静态方法loadFactoryNames方法,其代码如下:
它所调用的loadSpringFactories代码如下:
private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {Map<String, List<String>> result = cache.get(classLoader);if (result != null) {return result;}result = new HashMap<>();try {Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);while (urls.hasMoreElements()) {URL url = urls.nextElement();UrlResource resource = new UrlResource(url);Properties properties = PropertiesLoaderUtils.loadProperties(resource);for (Map.Entry<?, ?> entry : properties.entrySet()) {String factoryTypeName = ((String) entry.getKey()).trim();String[] factoryImplementationNames =StringUtils.commaDelimitedListToStringArray((String) entry.getValue());for (String factoryImplementationName : factoryImplementationNames) {result.computeIfAbsent(factoryTypeName, key -> new ArrayList<>()).add(factoryImplementationName.trim());}}}// Replace all lists with unmodifiable lists containing unique elementsresult.replaceAll((factoryType, implementations) -> implementations.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)));cache.put(classLoader, result);}catch (IOException ex) {throw new IllegalArgumentException("Unable to load factories from location [" +FACTORIES_RESOURCE_LOCATION + "]", ex);}return result;}
其中包含如下代码片段:
FACTORIES_RESOURCE_LOCATION 常量值为 “META-INF/spring.factories”,该方法给调用者返回一个List,
然后接着往下执行:
createSpringFactoriesInstances通过反射生成factories对象实例,
然后这个方法返回一个List,最后setInitializers方法把这个List设置到SpringApplication的成员变量initializers