Interviewer: Has the Spring Framework worked?

Xiao Bai: Yes (a little guilty, because there is too much content in the Spring framework).

Applicationgcontext. XML defines a bean with the id of authService. This bean is obtained through the getBean method of the ApplicationContext instance object.

Small white: When the Spring container starts, it parses applicationgContext.xml, parsing beans defined in the XML (such as authService) into Spring’s internal BeanDefinition. With beanName(for example, authService) as the key, BeanDefinition (such as authService corresponding BeanDefinition) as the value stored in the DefaultListableBeanFactory beanDefinitionMap of attributes (in fact it is a Concurrent Attribute of type HashMap), storing beanName in beanDefinitionNames (type List), then iterating beanName for beanDefinitionNames, instantiating the bean and populating the properties. During the instantiation, If a dependency is not instantiated, the dependency is instantiated first, and then the dependency itself is instantiated. After the instantiation, the instance is stored in the cache of the singleton bean. When the getBean method is called, the singleton bean is searched in the cache. Then you can use it directly.

Interviewer: What about the parsing process of XML files?

Small white: Specified in the code to load the XML file, after the Spring container in the process of initialization, through ResourceLoader interface implementation class, ClassPathXmlApplicationContext, for example, to convert the XML file path to the corresponding Resource file, For example, the ClassPathResource file is converted to a Document file using the DocumentLoader. Then through DefaultBeanDefinitionDocumentReader to parse the Document, and use BeanDefinitionParserDelegate to parse element to parse the XML bean definitions in each element, Put it in the BeanDefinition.

Interviewer: Can you elaborate on what a BeanDefinition is?

BeanDefinition: In order for the life cycle of an object to be managed by the Spring container, its class information must first be translated into Spring’s internal data structure. BeanDefinition is the data structure used to describe the class information of the object. For example, class name, scope, attribute, constructor parameter list, dependent bean, whether it is a singleton class, whether it is lazy loading, etc. In fact, the definition information of the bean is stored in the corresponding attribute of the BeanDefinition, and then the operation on the bean is performed directly on the BeanDefinition. For example, once you have the BeanDefinition, you can use reflection to create an object based on the class name, constructor, and constructor parameters. BeanDefinition is an interface. It is an abstract definition that actually uses its implementation classes, such as ChildBeanDefinition, RootBeanDefinition, GenericBeanDefinition, and so on. BeanDefinition inherits AttributeAccessor, which means it has the ability to process attributes. BeanDefinition inherits BeanMetadataElement, which means that it can hold a Bean metadata element that holds an Object corresponding to a Bean tag in an XML file.

Interviewer: did you have when it comes to DefaultListableBeanFactory, what is the role of it in the Spring framework?

Small white: DefaultListableBeanFactory is the core part of the Bean loading, is Spring registration and load the default implementation of the Bean. DefaultListableBeanFactory indirectly realized the BeanFactory interface, and in the BeanFactory interface defines the bean and many operations related methods, such as getBean, containsBean, isSingleton etc., So DefaultListableBeanFactory also hold the operation accordingly.

Interviewer: What’s a BeanFactory?

BeanFactory is the root interface used to access the Spring Bean container. It is a simple Bean factory, often referred to as the top definition of the IOC container, on which various IOC containers are extended to meet different needs, including the frequently used ApplicationContext.

Interviewer: How do you understand BeanFactory and FactoryBean?

BeanFactory: BeanFactory defines the most basic form of the IOC container and provides the most basic interface that the IOC container complies with. This is the lowest level and most basic programming specification that Spring IOC complies with. It is only an interface, not an implementation of the IOC container. Its responsibilities include instantiating, locating, configuring objects in an application, and establishing dependencies between these objects. In general, Spring instantiates beans using the bean’s class properties through reflection. However, in some cases, the process of instantiating beans is complicated and requires a lot of configuration information in the bean definition in the traditional way. Spring provides a factory class interface for FactoryBean. You can implement this interface to customize the logic of instantiating the Bean.

Interviewer: If you want to modify the properties of a bean before initialization, how do you do that?

Define a BeanFactoryPostProcessor that implements the BeanFactoryPostProcessor interface and implements the postProcessBeanFactory method, which modifies the bean properties before initialization.

Interviewer: How is this custom BeanFactoryPostProcessor called automatically?

Small white: In the process of the Spring container initialization is automatically triggered, specific code in AbstractApplicationContext will call invokeBeanFactoryPostProcessors method in the class, Filter out all the class names that implement the BeanFactoryPostProcessor interface in this method, and then iterate through the postProcessBeanFactory method that calls those implementation classes.

Interviewer: If you want to intercept the bean while it is being initialized and do additional initialization, how do you do that?

BeanPostProcessor: Define BeanPostProcessor to implement BeanPostProcessor interface. In this interface, define two methods: PostProcessBeforeInitialization and postProcessAfterInitialization. PostProcessBeforeInitialization method before the afterPropertiesSet and custom initialization method, by implementing this method, the method of internal for additional operations before initialization. PostProcessAfterInitialization method after the afterPropertiesSet and custom initialization method, by implementing this method, the method of internal extra operation after initialization.

Interviewer: Are all defined beans initialized during Spring container initialization?

By default, only uninitialized, non-lazy-loaded singleton beans are initialized. Beans with other scope values are initialized when they are used, as in Prototype.

Interviewer: Have you seen the source code for bean initialization in Spring?

Small white: see, singleton bean initialization, through reflection to create instance object, during the property filling, if the dependent object is not created, create the dependent object first, and finally add the bean instance into the cache of the singleton bean instance.

Interviewer: How does Spring address circular dependencies during bean instantiation?

Small white: Spring only for singleton bean circular dependencies are solved, at the same time, if is caused by the constructor injection cycles, the Spring is no way to solve, just throw BeanCurrentlyInCreationException anomalies. If it is a circular dependency created by setter injection, Spring creates a bean object by exposing an ObjectFactory in advance to return a bean object that is being created so that other beans can reference the bean.

Interviewer: What design patterns are used in the Spring framework?

Small white:…… Er…


Recommended reading:

Instead of concatenating strings with plus signs, the interviewer asked me why

Why isn’t SimpleDateFormat thread-safe

ThreadLocal is said to be a cliche, but why do interviewers keep asking questions

The interviewer and I spent half an hour talking about how Java annotations work

How do I get rid of the string of interview questions caused by multiple ifs in code

Quick git routine in three minutes

I almost fell to my knees

One line of code, all these questions

Interviewer: The JVM has optimized locks for what?

Synchronized serial q

Deep into Spring Boot (13) : Kafka integration details

In-depth understanding of Spring series 15: @async implementation principles

Click “I’m watching.