preface
In the previous article, I described how to build a source code reading environment. Now that you have built the source code environment and it works locally, let’s start reading the source code!
When reading the source code, will refer to the official documents, many concepts can be answered in the official website, interested partners can continue to read, as a review, write deficiencies, hope more guidance.
The IoC and DI
IoC
IoC (Inversion of Control)
Previously, new created other objects inside the object and then used.
Spring now has a container that can create and manage these objects and inject other objects that depend on them into the object. The creation and destruction of these objects are managed by Spring.
You are no longer in control of the life cycle of other objects than you were before, a process called inversion of control. The container that manages these classes uniformly is called the IoC container.
DI
IoC is also known as dependency injection (DI).
IoC is also called DI.
IoC and DI are two different ways of describing the same concept.
Dependency injection means that the dependencies between components are determined by the container at run time. Figuratively speaking, the container dynamically injects a dependency into the component.
Through the dependency injection mechanism, we can specify the resources needed by the target and complete our business logic through simple configuration without any code, regardless of where the specific resources come from and who implements them.
Spring implements IoC through DI.
The Container and Bean
A Bean is an object instantiated, assembled, and managed by the Spring IoC container.
You’ve all written or seen the following code:
/** * gets the object * from the container@author liuzhihang
* @date2020/4/6 19:02 * /
@Component
public class CustomBeanFactory implements ApplicationContextAware {
private static ApplicationContext ctx;
@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
ctx = ac;
}
public static Object getBean(String beanName) {
returnctx.getBean(beanName); }}Copy the code
The code logic is simple: get the named Bean from the container, where the ApplicationContext interface is the Spring IoC container.
Of course, ApplicationContext is an interface that has many implementations, and it also inherits from BeanFactory.
Although the BeanFactory is the most basic form of the IoC container, ApplicationContext extends it a lot and has all the functionality of a BeanFactory, and it is generally recommended to use ApplicationContext in preference.
conclusion
After learning the concepts of IoC, DI, containers and beans through the Spring official website, you can basically have a general process combined with common use.
Of course, this is only a very rough guess, whether it is correct, still need to continue to read the source code, and then to verify.
Related to recommend
- Spring source code learning 01: source code reading environment construction
- How did you resolve the Spring self-invocation transaction failure?
- The APP crashed inexplicably. At first, I thought it was the pot with the uppercase name in the Header, but finally I found it was the container’s fault