The general understanding and simple writing of Spring containers
Spring IOC
Posted by chyuan on
2022-11-13
Estimated Reading Time 6 Minutes
Words 985 In Total
Problem description:
Beginners are always confused when they are new to ‘Spring’ containers.
I don’t know why the object can be used directly “@Autowired” where it is wanted.
This article is a popular talk about how to use ‘@Autowired’ to get the object you want.
Think about why ‘Spring’ containers were introduced?
In actual development work, developers need to write a lot of ‘classes’ to complete a function and complete a transaction.
To use a ‘Class’ to work, we first have to ‘new’ an instance object of this ‘Class’, right, and will request space from the ‘JVM’ (Java Virtual Machine) before we can execute the non-static code sections written in the ‘Class’.
If there is no ‘Spring’ container, then every time the ‘new’ object finishes working, it has to be freed to avoid taking up space, right?
Then the problem comes, so repeatedly to the commonly used Class to new objects, to release objects, but not to let these commonly used Class keep an object for the system resource overhead is small. So the ‘Spring’ container came into being. It is to solve the development pain points here.
Popular understanding of ‘Spring’ container:
The actual function of the ‘Spring’ container, which teachers should have talked about, is to carry the object.
Be specific, for example, you can think of the ‘Spring’ container as a toolbox that can contain hammers, pliers, wrenches, tape measures, etc.
In fact, the ‘Spring’ container mainly does three things.
Create and persist object instances. **
Tag object instances for management. **
Provides an interface to the outside world to get the object through the markup of the instance object. **
Practice, dry! Don’t instigate:
Let’s implement a very rough Spring container with simple code, and you can understand it by reading it along.
Coding Preparation:
‘java JDK 1.8’ version
‘IDE’ tool, preferred ‘IntelliJ IDEA’ (this is charged, a little expensive, of course, I won’t tell you the way to crack, I publicly support the genuine version), or ‘Eclipse’, it is not recommended to use ‘MyEclipse’
Prepare a few classes:
Customize registration annotations:
1 2 3 4 5 6 7 8 9 10 11 12
package com.example.demo.annotation;
import java.lang.annotation.*;
/** * Created by CN94740284 on 2/9/2020. */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public@interface MyComponent { }
Customize binding annotations:
1 2 3 4 5 6 7 8 9 10 11 12
package com.example.demo.annotation;
import java.lang.annotation.*;
/** * Created by CN94740284 on 2/9/2020. */ @Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public@interface MyAutowired { }
privatestaticbooleanbindFields(Object obj,Field[] fields,Map<String, Object> tools)throws IllegalAccessException { for (Field field : fields) { //Gets the annotation of type T on the property if(field.isAnnotationPresent(MyAutowired.class)) { Class<?> type = field.getType(); Objecttool= tools.get(type.getName()); if(tool!=null){ field.setAccessible(true); field.set(obj,tool); }else{ returnfalse; } } } returntrue; }
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !