Spring source code parsing note
This article was compiled by Colodoo (Paper Umbrella)
QQ 425343603
Java Learning Exchange Group (717726984)
Refer to www.bilibili.com/video/BV1dK… 1-10 chapters
BeanDefinition
Spring creates Bean objects based on beanDefinitions. Beandefinitions have a number of properties that describe beans. Beandefinitions are very core concepts in Spring.
@Component, @Bean, are resolved to BeanDefinition.
Configuration properties include
- beanClass
- scope
- isLazy
- dependsOn
- primary
- initMethodName
beanClass
Represents the type of a bean, such as userService. class or orderService. class, from which Spring instantiates the object during bean creation.
scope
Represents the scope of a bean, for example:
Scope equals singleton, which is a singleton bean;
Scope equals prototype, and the bean is a prototype bean.
isLazy
The isLazy property of the prototype bean does not work. Lazy singleton beans are generated on the first getBean. Non-lazy singleton beans are generated directly during Spring startup.
dependsOn
Represents other beans that a bean depends on before it is created. Before a bean is created, all of the beans on which it depends must be created.
primary
In Spring, a type can have more than one bean object. During dependency injection, if multiple beans are found by type, it will determine whether there is a primary bean in these beans. If so, it will directly inject the bean into properties.
initMethodName
In the life cycle of a bean, there is a step called initialization. Spring will call the initialization method of the bean in this step. The initialization logic is controlled by the programmer himself, indicating that the programmer can customize the logic to process the bean.
BeanFactory
BeanFactory is a Spring container. BeanFactory translates to Bean factory. It can be used to create and retrieve beans, and the BeanFactory is a very core component of Spring.
BeanDefintion, BeanFactory, Bean relationships
BeanFactory will use the BeanDefinition to generate Bean objects. BeanDefinition is the raw material of the BeanFactory, and Bean objects are the products produced by the BeanFactory.
BeanFactory produces Bean objects using BeanDefinition.
Core subinterfaces and implementation classes
- ListableBeanFactory
- ConfigurableBeanFactory
- AuwowireCapableBeanFactory
- AbstractBeanFactory
- DefaultListableBeanFactory
DefaultListableBeanFactory
Support for singleton beans, support for Bean aliases, support for parent-child BeanFactories, support for Bean type conversion, support for Bean post-processing, support for FactoryBeans, support for autowiring, and much more.
Bean life cycle
Phases of the life cycle:
- BeanDefinition -> Bean definition
- Constructor inference -> Select constructor
- Instantiate -> Constructor reflects the instantiated object
- Property Fill -> Automatically fill properties
- Initialize -> Assign and verify other attributes
- After initialization -> AOP, generate proxy objects
BeanDefinition
BeanDefinition represents a Bean definition. It defines the type of a Bean. Spring uses BeanDefinition to create beans. For example, you need to use the beanClass attribute in BeanDefinition to determine the type of the Bean to instantiate the object.
Construction method inference
There can be more than one constructor in a Bean, and Spring is required to determine which constructor to use. This process is too complicated for space to cover. Once a constructor has been determined after inference through the constructor, an object can be instantiated using the constructor.
instantiation
An instantiation object is obtained through constructor reflection, and in Spring, instantiation can be interfered with through the BeanPostProcessor mechanism.
fill
The instantiated object is an “incomplete” object. “Incomplete” means that some properties of the object have not been populated, that is, Spring has not automatically assigned values to certain properties. Property padding is what we call automatic injection, dependency injection.
Initialize the
After an object’s properties are populated, Spring provides an initialization mechanism that programmers can use to customize the Bean, such as using the InitializingBean interface to assign values to other properties in the Bean or to validate certain properties in the Bean.
@Autowired
@autoWired indicates whether a property needs dependency injection, which can be written on properties and methods. The required attribute in the annotation defaults to true, indicating that an exception is thrown if no object can be injected into the attribute.
@Service
public class OrderService {
@Autowired
private UserService userService;
}
Copy the code
@Autowired is added to an attribute. Spring will automatically assign the attribute value based on the object instantiated by adding @Autowired to the attribute in the attribute filling step during the Bean life cycle.
Tip: The operation step is in the property population step.
A constructor
When @autoWired is applied to a constructor, Spring selects the constructor to instantiate in the inference constructor phase. Before reflection calls the constructor, it finds the Bean object from the Spring container based on the constructor parameter type and parameter name and uses it as the constructor input parameter.
@Resource
@resource If the name attribute has a value, Spring will directly find the Bean object from the Spring container based on the specified name value. If found, it succeeds. If not, an error is reported.
@Value
The @Value annotation is similar to @Resource and @AutoWired in that it is used for dependency injection of Properties, except that @Value is used to get values from Properties files and @Value can parse SpEL (Spring expressions).
FactoryBean
Spring provides a flexible way to create beans by implementing the getObject() method in the FactoryBean interface to return an object that is the final Bean object.
Interface methods:
- Object getObject(): Returns a Bean Object
- Boolean isSingleton(): Returns a singleton Bean object
- Class getObjectType(): Returns the type of the Bean object
Code:
@Component( "demo")
public class DemoFactoryBean implements FactoryBean {
@Override
public 0bject get0bject(a) throws Exception {
return new Demo();
}
@Override
public class<? >get0bjectType(a){
return Demo.class;
}
@Override
// Is the defined Bean a singleton or a stereotype
public boolean issingleton(a) {
return true; }}Copy the code
FactoryBean mechanisms are widely used within Spring and in Spring’s integration with third-party frameworks or components.
FactoryBean and the BeanFactory
FactoryBean
The FactoryBean object is itself a Bean and acts as a small factory that can produce additional beans.
BeanFactory
BeanFactory, a Spring container, is a large factory that produces a wide variety of beans.
ApplicationContext
ApplicationContext is a more powerful Spring container than BeanFactory. It can create and fetch beans, and it supports internationalization, event broadcasting, and resource fetching, among other things that BeanFactory does not.
Inherited interface
- EnvironmentCapable
- ListableBeanFactory
- HierarchicalBeanFactory
- MessageSource
- ApplicationEventPublisher
BeanPostProcessor
BeanPostProcessor is an interface in Spring. We define a postprocessor that provides a class to implement this interface. There are also some interfaces in Spring that inherit BeanPostProcessor. These sub-interfaces are built on the BeanPostProcessor and add some additional functionality.
Methods in BeanPostProcessor
PostProcessBeforelnitialization () : initialization method before, said this method can be used to the beans before initialization customized processing.
PostProcessAfterlnitialization) : after initialization method that can use this method to the Bean after initialization customized processing.
InstantiationAwareBeanPostProcessor
A subinterface BeanPostProcessor postProcessBeforelnstantiation () : before instantiation
PostProcessAfterlnstantiation () : instantiated postProcessProperties () : properties after injection
AOP
Aop is sectioning oriented programming, is a very suitable for without modifying business code under the premise of a certain or some business to add unified functions, such as logging, permission reset, transaction management, can be very good to make code decoupling, improve development efficiency.
- Advice
- Pointcut
- Advisor
- Weaving
- Target
- Join Point
Advice
Advice can be understood as Advice, Advice, and you define the proxy logic in Spring by defining Advice.
Pointcut
The Pointcut is the Pointcut that indicates which class and method the Advice proxy logic is applied to.
Advisor
Advisr is equal to Advice+Pointcut, which represents the whole of broker logic and Pointcut. Programmers can define both Pointcut and broker logic by defining or encapsulating an Advisor.
Weaving
Weaving stands for Weaving, and the process of embedding Advicedialing logic into a section at the source code level is called Weaving.
Target
Target represents the Target object, that is, the proxied object, held against the Target object in an AOP generated proxy object.
Join Point
A Join Point represents the Join Point, and in Spring AOP, the execution Point of a method.
How AOP works
AOP occurs during the Bean lifecycle;
- When Spring generates bean objects, it instantiates an object, the target object.
- The target object is then populated with properties.
- During the initialization step, the target object is determined to have a corresponding slice.
- If there are facets, that means the current target object needs AOP.
- A proxy object is generated through Cglib or the JDK dynamic proxy mechanism as the final bean object.
- There is a target attribute in the proxy object that only thinks about the target object.