In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "java Spring source code analysis", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "java Spring source code analysis" bar!
The most important feature in Spring is Ioc, and you might even say aop. In fact, the implementation of aop is also based on ioc. Ioc (Inversion of Control), that is, "inversion of control", is not a technology, but a design idea. In Java development, Ioc means handing over your designed objects to the control of the container, rather than the traditional direct control within your objects.
There are a lot of articles about Spring IOC source code analysis online, and now I'm going to find another way. Spring Ioc object twist and the core interfaces involved to analyze its source code implementation.
I divided the object conversion of Spring Ioc into the following four steps:
Resource-> BeanDefinition-> BeanWrapper-> Object
1 Resource
Resouce is actually an interface, representing resources, and what is needed to move from one place to another in a computer is data flow, so Resource implements the InputStreamSource interface, and you can get Inputstream through the InputStreamSource interface, so you can read different Bean definitions.
Public interface InputStreamSource {InputStream getInputStream () throws IOException;}
Spring can define different types of bean, which can finally be encapsulated into Resource and read through the IO stream. Spring can define a type of bean object:
XML: this is the form in which Spring first defined bean
Annotation: due to the complexity of defining bean through XML, Spring has been improved to define bean through @ Component and annotations based on it. For example: @ Service,@Controller, etc., all of which can define bean, but the semantics are more explicit.
Class: defined by @ Configuration and @ Bean annotations, @ Configuration proxies xml resource files, and @ Bean replaces tags.
Properties/yml: define bean through @ EnableConfigurationProperties and @ ConfigurationProperties. This form is widely used in Spring boot automatic injection.
2 、 BeanDefinition
It is obvious that this is the definition of the Bean object. Spring defines bean in different forms, and eventually translates these definitions into BeanDefinition, which is stored in a Spring container for dependency injection. Let's take a look at the interface definition of BeanDefinition.
The definition of this interface is complex, but for the initial understanding of spring ioc, you only need to care about two methods.
GetConstructorArgumentValues: gets the parameters defined by the constructor injection.
GetPropertyValues: gets the parameters defined by setter injection.
So Spring supports constructor injection and setter dependency injection.
3 、 BeanWapper
In fact, what is dependency injection? to put it simply, Spring helps us create objects. The creation object is written in the Java file so that different values can be injected through different Spring configurations. The responsibility for creating objects has changed from a Java file to a Spring configuration file.
Let me ask you a simple question, how to create an object. Maybe everyone laughs, it's not easy to create an object.
In fact, Spring also creates objects in this way. If you don't believe me, let's see: (entry method BeanFactory#getBean)
AbstractAutowireCapableBeanFactory#createBeanInstance (): creates an object instance by reflecting Constructor calls that are configured with or without parameters. Get it through BeanDefinition#getConstructorArgumentValues and return the BeanWrapper object.
AbstractAutowireCapableBeanFactory#populateBean (): get all the defined setter injected attributes (BeanDefinition#getPropertyValues) generated by the defined bean, then iterate through some of these attributes, get all the PropertyDescriptor of the object through introspection, and get the PropertyDescriptor#getWriteMethod method of the property, that is, the setter method, which depends on the injected value. If it is a common property or some complex object, such as ordinary properties String, int, long or classpath:*, etc., it can be directly injected; for reference type objects, continue dependency injection until all properties are ordinary properties.
AbstractAutowireCapableBeanFactory#initializeBean (): call Spring custom initialization methods such as BeanPostProcessor extension and init-method.
The instantiation object returns BeanWrapper for dependency injection service, which is the second step above. The function of this interface is still very complex, it inherits four interfaces.
TypeConverter
PropertyEditorRegistry
PropertyAccessor
ConfigurablePropertyAccessor
Let's introduce the functions of these interfaces respectively.
3.1 TypeConverter
Here is the definition of this interface.
Its purpose is to automatically cast types, because Spring is too imperceptible. You may not feel it yet. It doesn't matter. I'll give you a hint and you should understand. For example, declare a user object that has both the name of type String and the age of type Int. How does Spring know the type of attribute? This is the automatic type conversion of Spring. I have analyzed the automatic type conversion of Spring before.
3.2 PropertyEditorRegistry
The main function of this interface is to register the property modifier (PropertyEditor), which is the mechanism within Java introspection.
Custom type conversions are generally achieved by inheriting java.beans.PropertyEditorSupport. There are a large number of implementations within Spring, as shown in the following figure
3.3 PropertyAccessor
PropertyAccessor this interface is to determine whether an attribute in an object is readable / writable, and can set or read the value of an attribute. From this interface definition, we can see that its use is really used for dependency injection. The write operation of the property operation is then invoked, which is completely dependent on injection.
3.4 ConfigurablePropertyAccessor
The function of this interface is the same as that of the PropertyEditorRegistry interface, except that the latter does type conversion automatically through java introspection, while the ConfigurablePropertyAccessor interface does type conversion through Spring's own defined org.springframework.core.convert.ConversionService. DefaultConversionService is used as automatic type conversion support by default in Spring, and many default type conversions have been added internally.
I have analyzed the automatic type conversion of Spring before. The principle is the same as java introspection.
3.5 BeanWrapper
This interface is quite simple, with dependency injection, type conversion registration (java introspection or automatic type conversion customized by Spring) by implementing the above interfaces. Then the main function of this interface is to get the current instance object by calling the getWrappedInstance method and provide it to the writer method of the property for dependency injection.
WriteMethod.invoke (getWrappedInstance (), value); thank you for reading, the above is the content of "java Spring source code analysis", after the study of this article, I believe you have a deeper understanding of java Spring source code analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.