In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to write a spring IOC container. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.
There is no doubt that the basic core and starting point of the spring framework is the core technology provided by IOC,IOC as a spring container, which successfully completes the reversal of dependencies: from the active management of dependencies in the main class to the global control of dependencies in the spring container. Today we will write a spring IOC container by hand.
Step 0 (preparation):
(1) JDK1.8 and java environment variables
(2) maven package and maven environment variables
(3) idea
Step 1 (create the project and introduce the tomcat package)
PS: because the purpose of this project is to simulate some of the functions of spring, it is named spring
(1) the content of the created project is as follows:
(2) the initial state of the pom file is as follows:
Step 2: build the main framework
PS: a normal spring project is generally divided into controller and service parts. Database links are ignored here.
(1) where TestController implements the function, returns name+ "Hello World!" and references TestService
(2) while TestService and TestServiceImpl realize the function respectively.
Step 3: create and label
PS: we need many tags, such as service,controller, autowired, etc.
(1) create various tags (temporarily create these tags, but there is not enough to add later)
(2) label
Since RequestMpping needs to be followed by a path, its value () method needs to be overridden here, as follows:
Step 4: get down to business and create a DisPatcherServlet class to implement the IOC container function
(1) DisPatcherServlet inherits HttpServlet (pay attention to adding dependencies) and overrides the doGet,doPost method
(2) create a doDispatcher method and reference it by doPost
Step 5: initialize
(1) pause the implementation of doDispatcher and rewrite the initialization method init. Initialization is divided into five steps, as follows
Note: add the attribute class (Properties) at the top, add application.properties to the resource and add the scanPackage attribute to the root directory to be scanned.
(2) to load the configuration file, you need to configure a web.xml first. The path and content are as follows:
Path:
Content:
You should be able to understand the rest of the content.
(3) next, use recursion to store all class paths into a classPaths.
(4) next, put the classes tagged with controller and service into the ioc container
(5) next, inject (get the instance object from the IOC container through the Autowired tag and assign a value)
(6) get all the RequestMapping content and write it to a collection of objects
Step 6: go back to the steps of implementing doDisPatcher
(1) first, set 404 (class notfound)
(2) the second half, realize the interface function.
PS1: the method of judging url
PS2: get the method according to url
PS3: get all the parameters under the method
Final step: execute tomcat
...
So
(1) do you think the above is correct?
Do you think you can get the results you want?
How naive!
If you run it the way I say, congratulations, the result is not thanks thanks! The real challenge has only just begun:
First, the definition of the tag class, as follows, particularly important is Target
We need to add a mapping.
Second, in the TestController class, RequestParam is missing the value attribute, and if not, the value cannot be passed.
Third, is there a problem with parameter initialization? Pay attention to this code. What should the name of the parameter be related to?
Okay, now we can really make it.
Let's see the results.
After reading the above, do you have any further understanding of how to write a spring IOC container? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.