Handwriting IOC

1. IOC analysis

1. What is IOC?

IOC: Inversion of Control, also known as dependent inversion (inversion)

Question: How to understand control reversal?

Reverse: The acquisition of dependent objects is reversed. Created by yourself, inverted to get from IOC container (and automatic injection)

2. What benefits does the IOC container bring?

1) The code is more concise, and there is no need to go to the object that new needs to use.

2) Interface-oriented programming, users are decoupled from specific classes, easy to extend and replace implementers.

3) AOP can be easily enhanced. The premise of AOP is IOC

3. What does the IOC container do?

The work of the IOC container: responsible for creating and managing class instances and providing instances to users.

4. Is the IOC container an example of the factory model?

Yes. The IOC container is responsible for creating class instance objects. The user needs to get the instance from the IOC container. The IOC container is also called a bean factory.

2. IOC design and implementation

1. The work of the IOC container

Create and manage beans. It is a factory responsible for providing bean instances to the outside world.

Q: What is a bean?

Beans are components, they are class objects!

img

1) What behavior (external interface) should the IOC container have?

Provide bean instance externally, getBean () method

2) Does the getBean () method require parameters? How many parameters and what types of parameters are needed?

In the simple factory model, when a factory can create many types of products, if you want to create a certain type of product, you need to tell the factory.

3) What type should the return value of this getBean () method be?

All kinds of beans can only be Object.

After the above problem, the interface of the Bean factory can be defined! ! !

2. Bean factory interface:

img

How does the Bean factory know how to create a bean?

img

How to tell Bean factory?

Is a definition registration, we can define a bean definition registration interface for it

3. Bean defines the registration interface

img

4. Bean definition

img

Question 1: What is the purpose of bean definition?

Tell the bean factory how to create a certain type of bean

Question 2: What are the ways to obtain an instance of a class?

new constructor

Person p = new Person();

Factory method

still

public class PersonFactory {
    public static Person getPerson() {
        return new Person();
    }
}

Member methods

public class PersonFactory {
    public Person getPerson() {
        return new Person();
    }
}

Question 3: What information does the Bean factory need to know when it creates beans for us?

1) When creating a bean by the new constructor, the Bean factory needs to know the class name of the class to be created

2) When creating beans in a static factory method, the Bean factory needs to know the factory class name and factory method name

3) When a bean is created by a member factory method, the bean factory needs to know the factory class name (factory bean name) and factory method name

Because you need to get the factory class object to call the factory method name to create the bean, so directly create the factory bean object for the factory bean name

Question 4: Do I need to create a new one every time I get a bean instance from the bean factory?

No, some only need a single instance

Question 5: The Bean definition is used to create beans for the Bean factory, so what methods should the Bean definition interface provide to the Bean factory?

When you create a bean by the new constructor method, you need to tell the bean factory how to get the class name- get the bean class name: getBeanClass (): Class

When creating a bean in a static factory method, you need to tell the bean factory how to obtain the factory method name: getFactoryMethodName (): String

When creating a bean in a member factory method, you need to tell the bean factory how to obtain the factory bean name: getFactoryBeanName (): String

Whether it is a singleton method: getScope (): Sting, isSingleton (), isPrototype ()

Question 6: Class objects are handed over to the IOC container for management. Is there any life stage thing to do in the life cycle of class objects?

For example, after creating an object, some initialization may be required

Some objects may need to perform some specific destruction logic when they are destroyed (such as releasing resources)

Then provide in the Bean definition to allow users to specify initialization and destruction methods.

For the Bean factory, you need to provide getInitMethodName (), getDestroyMethodName ()

5. Bean definition interface

img

We continue to look at the following figure:

img

Description: The bean definition BeanDefinition is registered to the Bean factory BeanFactory through the bean definition registration interface BeanDefinitionRegistry, and the Bean factory BeanFactory is responsible for creating the bean

6. BeanFactory implementation

Implement a most basic default bean factory: DefaultBeanFactory

img

Description:

6.1 Implement bean definition information registration interface

Question 1: How to store bean definition information?

Map

Question 2: Can the bean definition be renamed? What about duplicate names?

Abnormal name throw

6.2 Implementing the bean factory

Question 1: What is the storage for the created beans to facilitate the next acquisition?

Map, because getBean is taken by name, it is better to put it in Map

Question 2: What to do in the getBean method?

Create a bean instance and initialize

6.4 Extending DefaultBeanFactory

For singleton beans, we can instantiate in advance. The advantage of this is that we do n’t need to fetch them when needed, which can ensure thread safety and improve performance.

img

3. DI analysis

DI (Dependency Injection) dependency injection analysis

Question 1: Where will there be dependence?

Construction parameter dependence

Attribute dependency

Question 2: What is the nature of dependency injection?

Assignment, give the value of the construction parameter, assign value to the property

Question 3: What are the possible values ​​of parameter values ​​and attribute values?

Direct value, bean dependency

Examples:

public class Girl{
    public Girl(String name,int age,char cup,Boy boyfriend){
    }  

}

name, age, cup are direct values, boyfriend is bean dependent

Question 4: What are the situations of direct value?

Basic data types, String

Array, collection

Properties

Map

Essence: Parameter values ​​and attribute values ​​are all values. When the bean factory performs dependency injection, it is giving the value.

Fourth, DI implementation

1. Construction parameter dependence

1.1 Dependency analysis of construction parameters

public class Girl{
    public Girl(String name,int age,char cup,Boy boyfriend){
    }  

}

Question 1: How do we create a Girl?

Boy  leeSmall = new Boy("leeSmall");
Girl girl = new Girl("小青",18,'D',leeSmall);

Just pass the value directly into the constructor

Question 2: Can we define the construction parameter dependency like this?

The first parameter value is: "Xiaoqing"

The second parameter value is: 18

The third parameter value is: 'D'

The fourth parameter value is: dependent on a Boy Bean

absolutely okay!

Construction parameter dependent design

Question 1: There can be multiple parameters, what storage?

Collection: List

Question 2: The parameters have an order, how to reflect the order?

Put into List in order of parameters

Question 3: The parameter can be a direct value or a bean dependency, how to express it?

Because there can be multiple values, you can only use Object

List constructorArgumentValues

Question 4: If Object is used to represent the value, how to distinguish between Bean dependency?

Define a data type BeanReference for the Bean dependency. When the bean factory constructs the Bean instance, iterate to determine whether the parameter is BeanReference, and if so, replace it with the dependent bean instance.

Question 5: If the direct values ​​are arrays, collections, etc., and some of their elements are bean dependencies, how to deal with them?

The element value is still BeanReference, and the bean factory needs to be traversed and replaced when it is used.

1.2 BeanReference

BeanReference is used to illustrate the dependency of the bean: which bean to depend on

img

1.3 Add a method to obtain the construction parameter value in BeanDefinition

img

With the construction parameter dependency, we can implement the construction parameter dependency injection below!

1.4 Implementation of dependency injection of construction parameters in BeanFactory

1.4.1 First, you need to convert the reference of the construction parameter in the bean definition to a real value, and add a method in DefaultBeanFactory to do this.

img

Question: With parameters, how to determine which constructor and which factory method? Need to consider the following

a) The method can be overloaded

b) The formal parameter may be an interface or a parent class, and the actual parameter is a specific child implementation

c) The construction methods and methods provided by reflection can be obtained as follows:

java.lang.Class

Judgment logic:

a) First perform an exact match search according to the type of the parameter, if not found, then perform the second step search;

b) Obtain all the traversal of the construction method, filter by the number of parameters, and then compare the formal parameter type with the actual parameter type

1.4.2 After we have determined the construction method or factory method, can we omit the judgment for the next acquisition of the prototype bean?

In other words, for the prototype Bean, we can cache this constructor or factory method. How to achieve?

1.4.3 Next, you can write code to find the constructor or find the factory method

1) Add a method to find the constructor in DefaultBeanFactory

2) Modify the code for creating an instance in the DefaultBeanFactory using the constructor to call determineConstructor
3) Modify the static factory method and factory method parameter dependency according to the way to increase the search constructor

Modify the code in DefaultBeanFactory to create an instance with a factory method to call determineFactoryMethod

1.4.5 How to deal with circular dependency

Question: Can you rely on circularity when constructing objects?

The circular dependency when constructing an instance object will fall into a deadlock situation, and the circular dependency when constructing an instance is not allowed . So how do you find circular dependencies?

Found a method of circular dependency: add a record of the bean being constructed, each bean is added to the record at the beginning of construction, and removed from the record after construction. If there is a dependency, first see if the dependent bean is in the construction, if so, it forms a circular dependency, throwing an exception

2. Attribute dependence

Question 1: What is attribute dependency?

A certain attribute depends on a certain value, this value needs to be given externally

Question 2: How to describe an attribute dependency?

Attribute name, value, define one to represent these two values

Question 3: There will be multiple attribute dependencies, how to store?

List storage

Question 4: Is the attribute worthwhile situation the same as the construction parameter worthiness situation?

same

2.1 Defining attribute-dependent entities

The attribute dependent entity class diagram is as follows:

img

2.2 Add a method to get attribute dependency definition in BeanDefinition interface

2.3 Add corresponding implementation in GenericBeanDefinition

2.4 Implementation of attribute dependency in DefaultBeanFactory

In doGetBean (String beanName), add the call to set property dependency and put it at the end of the method.

Describe an attribute dependency? **

Attribute name, value, define one to represent these two values

Question 3: There will be multiple attribute dependencies, how to store?

List storage

Question 4: Is the attribute worthwhile situation the same as the construction parameter worthiness situation?

same

2.1 Defining attribute-dependent entities

The attribute dependent entity class diagram is as follows:

[External link image is being transferred ... (img-T6AgK1bl-1586693628403)]

2.2 Add a method to get attribute dependency definition in BeanDefinition interface

2.3 Add corresponding implementation in GenericBeanDefinition

2.4 Implementation of attribute dependency in DefaultBeanFactory

In doGetBean (String beanName), add the call to set property dependency and put it at the end of the method.

Note: Attribute dependency allows circular dependency, because the value of attribute dependency is set after the instance is created! ! !

Article from:
https://www.cnblogs.com/leeSmall/p/10023593.html

Published 37 original articles · won praise 6 · views 4649

Guess you like

Origin blog.csdn.net/littlewhitevg/article/details/105475206