Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand that META-INF/spring.factories in springboot2.0.6 obtains the fully qualified name of the corresponding class through the system loading class

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to understand how to understand META-INF/spring.factories in springboot2.0.6 to obtain the fully qualified name of the corresponding class through the system loading class, the content of the article is of high quality, so the editor shares it for you to do a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

In SpringBoot, you get all the META-INF/spring.factories files under the classpath through the getSpringFactoriesInstances (Class type) method, and then find the fully qualified name list of the corresponding class based on the type value. Let me analyze how the getSpringFactoriesInstances (Class type) method works

Source code parsing getSpringFactoriesInstances () method public class SpringApplication {private Collection getSpringFactoriesInstances (Class type) {return getSpringFactoriesInstances (type, new Class [] {});} / / get Spring factory private Collection getSpringFactoriesInstances (Class type, Class [] parameterTypes, Object... Args) {/ / get ClassLoader ClassLoader classLoader = Thread.currentThread () .getContextClassLoader (); / / Use names and ensure unique to protect against duplicates / / define the class array, that is, the return value names is the parent node defined in all the META-INF/spring.factories under the classpath (figure 2) Set names = new LinkedHashSet (SpringFactoriesLoader.loadFactoryNames (type, classLoader)) / / the inner loop initializes the constructor of names to get the instance object (figure 2) List instances = createSpringFactoriesInstances (type, parameterTypes, classLoader, args, names); AnnotationAwareOrderComparator.sort (instances); return instances } / / create Spring factory instance private List createSpringFactoriesInstances (Class type, Class [] parameterTypes, ClassLoader classLoader, Object [] args, Set names) {List instances = new ArrayList (names.size ()) / / Loop the value of names for (String name: names) {try {/ / (figure 3) / / get the corresponding Class object Class instanceClass = ClassUtils.forName (name, classLoader); Assert.isAssignable (type, instanceClass); Constructor constructor = instanceClass .getDeclaredConstructor (parameterTypes) by loading the corresponding class with the specified Class / / create an instance T instance = (T) BeanUtils.instantiateClass (constructor, args); instances.add (instance);} catch (Throwable ex) {throw new IllegalArgumentException ("Cannot instantiate" + type + ":" + name, ex);}} return instances;}}

The getSpringFactoriesInstances method obtains the system load class through the loadFactoryNames method of the SpringFactoriesLoader class, goes to all the META-INF/spring.factories files under the classpath to obtain the fully qualified name combination of the corresponding class, and iterates through the collection pair to create an instance by executing the createSpringFactoriesInstances method. Then return to the collection of instance objects.

Names to get the values of applicationContextInitializer-related load classes in all the META-INF/spring.factories files under the classpath

List of Spring factory instances created by instances to traverse names

Taking getSpringFactoriesInstances (ApplicationContextInitializer.class) as an example for debug analysis

Figures 1-1 and 1-2 mark all fully qualified names corresponding to ApplicationContextInitializer.class in META-INF/spring.factories files under all classpath

(figure 1-1)

(figure 1-2)

(figure 2) get the applicationContextInitializer-related factory class in the spring.factories file according to the class name "applicationContextInitializer" and initialize it)

(figure 3) initialize and create an instance based on the class name

In the getSpringFactoriesInstances method of the SpringApplication class, enter the loadFactoryNames method public abstract class SpringFactoriesLoader {public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories" of the SpringFactoresLoader class; private static final Map cache = new ConcurrentReferenceHashMap (); / / load factory public static List loadFactoryNames (Class factoryClass, @ Nullable ClassLoader classLoader) {/ / get the class name (figure 4) String factoryClassName = factoryClass.getName () / / get the factory class name return loadSpringFactories (classLoader) .getOrDefault (factoryClassName, Collections.emptyList ()) to be loaded according to the class name;} / / scan the loading class by loading all the META-INF/spring.factories files under the classpath. (figure 8) private static Map loadSpringFactories (@ Nullable ClassLoader classLoader) {/ / get the result set of the instance from cache if Null indicates that the current cache is empty Cache implements new ConcurrentReferenceHashMap () MultiValueMap result = cache.get (classLoader); if (result! = null) {return result } try {/ / get the resource urls in all the META-INF/spring.factories under the classpath (figure 5) / / call the getResouurces method to get / / when the classLoader is not empty, call the ClassLoader.getSystemResouurces method to get Enumeration urls = (classLoader! = null? ClassLoader.getResources (FACTORIES_RESOURCE_LOCATION): ClassLoader.getSystemResources (FACTORIES_RESOURCE_LOCATION); result = new LinkedMultiValueMap (); / / Loop the element in urls to get the element while (urls.hasMoreElements ()) {/ / get the element url address (figure 6) URL url = urls.nextElement (); UrlResource resource = new UrlResource (url) / / parsing file turns file into configuration attribute (figure 6) Properties properties = PropertiesLoaderUtils.loadProperties (resource) / / parse in a loop and put the result in result for (Map.Entry entry: properties.entrySet ()) {/ / class name list (figure 7) List factoryClassNames = Arrays.asList (StringUtils.commaDelimitedListToStringArray ((String) entry.getValue (); result.addAll ((String) entry.getKey (), factoryClassNames) }} / / result set of cache class loader and file parser cache.put (classLoader, result); return result;} catch (IOException ex) {throw new IllegalArgumentException ("Unable to load factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex) Take loadFactoryNames (ApplicationContextInitializer.class,classLoader) as an example for debug analysis

(figure 4) get the full name of class

(figure 5) get the urls of all META-INF/spring.factories files under classpath

(figure 6) get the specific location of the spring.factories file and the contents of the file)

(figure 7) get the list of corresponding classes in the contents of the spring.factories file

(figure 8)

Summary

The META-INF/spring.factories file under the classpath is loaded by classLoader, and the file information is parsed into configuration attributes and stored in the result set. According to the fully qualified name of the type, the result set corresponding to the type is obtained from the result set. The loop facilitates that the result set creates an instance based on the fully qualified name. Returns after sorting the collection of instances

On how to understand the springboot2.0.6 META-INF/spring.factories through the system load class to get the corresponding fully qualified name of class to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report