Hand in hand to teach you to write a spring IOC container

Huawei cloud developer community 2021-04-08 11:35:19
hand hand teach write spring


Abstract :spring The core and starting point of the framework is undoubtedly IOC,IOC As spring The core technology provided by container , Successfully completed the inversion of dependency : From the active management of dependency of the main class to spring Container global control over dependencies . Today I will take you to write a spring IOC Containers .

spring The core and starting point of the framework is undoubtedly IOC,IOC As spring The core technology provided by container , Successfully completed the inversion of dependency : From the active management of dependency of the main class to spring Container global control over dependencies . Today I will take you to write a spring IOC Containers .

The first 0 Step ( preparation ):

(1)JDK1.8 And java environment variable
(2)maven Package and maven environment variable
(3)idea

The first 1 Step ( Create project , introduce tomcat package )

PS: Because the purpose of this project is to simulate spring Part of the function of , So it's called spring

(1) The content of the created project is shown in the following figure :

(2)pom The initial state of the file is as follows :

The first 2 Step : Build the main frame

PS: A normal spring Projects are generally divided into controller and service part , Ignore database links for now

(1) among ,TestController Realization function , return name+“Hello World!”, quote TestService

(2) and TestService and TestServiceImpl The functions are realized respectively

The first 3 Step : establish , And label

PS: We need a lot of labels , Such as service,controller, also autowired etc.

(1) Create labels ( Create these tags temporarily , It's not enough to add )

(2) tagged

 

 

because RequestMpping You need to follow the path , So we need to rewrite it here value() Method , as follows :

The first 4 Step : Get to the point , Create a DisPatcherServlet class , Used to implement IOC Container function

(1)DisPatcherServlet Inherit HttpServlet( Pay attention to adding dependencies ), And rewrite doGet,doPost Method

(2) Create a doDispatcher Method , And be doPost quote

The first 5 Step : initialization

(1) Pause implementation doDispatcher, To override the initialization method init, Initialization is divided into five steps , as follows

Be careful : Add the attribute class at the top (Properties), And resource To add application.properties, And plus scanPackage attribute , The content is the root directory to be scanned

(2) Load configuration file implementation , You need to configure one first web.xml, The path and content are as follows :

route :

Content :

The rest , You should be able to understand it

(3) next step , Using recursion , Will all class Path to a collection (classPaths) Inside

(4) next step , Will be hit by controller and service Label the class into ioc In the container

(5) next step , Inject ( adopt Autowired Label from IOC Get instance object in container and assign value )

(6) Get all RequestMapping Content , And write it to a collection of objects

The first 6 Step : Come back to realize doDisPatcher Steps for

(1) First , Set up 404(class notfound)
(2) The back half , Realize the interface function
PS1: Judge url Method
PS2: according to url Access method
PS3: Get all the parameters under the method

The last step : perform tomcat

so,

(1) You think the above is right ?
(2) Do you think you can get the result you want ?

It's naive !!!

If you run it the way I say , congratulations , And what you get is not found!!! The real challenge is just beginning :

First of all , Definition of label class , as follows , Of particular importance Target

There's one more thing to add mapping

second ,TestController Class ,RequestParam Less value attribute , without , The value cannot be transferred .

Third , So parameter initialization , Is there a problem ? Notice this code , What should the name of Chuanshen be related to ?

Okay Now it's really going to work ~~~

Look at the results .

End of the flower ~~~

  This article is shared from Huawei cloud community 《 I'll teach you to write a spring IOC Containers 》, Original author : Technical torchbearer .

 

Click to follow , The first time to learn about Huawei's new cloud technology ~

版权声明
本文为[Huawei cloud developer community]所创,转载请带上原文链接,感谢
https://javamana.com/2021/04/20210408110425981V.html

  1. 【TTS】传输表空间AIX asm -> linux asm
  2. 【TTS】传输表空间Linux asm -> AIX asm
  3. 【DB宝40】MySQL高可用管理工具Orchestrator简介及测试
  4. 【TTS】传输表空间Linux ->AIX 基于rman
  5. 一本关于HTTP的恋爱日记
  6. 【RocketMQ源码分析】深入消息存储(3)
  7. SpringCloud+Nacos实现服务配置中心(Hoxton版本)
  8. SICP:构造过程抽象--面向对象的解释
  9. 3w 字长文爆肝 Java 基础面试题!太顶了!!!
  10. Spring Cloud 升级之路 - 2020.0.x - 3. Undertow 的 accesslog 配置
  11. win10卸载mysql5.7
  12. MySQL 批量插入,如何不插入重复数据?
  13. k8s cronjob应用示例
  14. 非常规方法,轻松应对Oracle数据库危急异常
  15. Oracle hang 之sqlplus -prelim使用方法
  16. 如何全文搜索oracle官方文档
  17. Java student achievement management system course design, with source code!
  18. win10安装mysql8.0
  19. 手把手教你写一个spring IOC容器
  20. JAVA 中的异常(1)- 基本概念
  21. A love diary about http
  22. navicat连接win10 mysql8.0 报错2059
  23. [rocketmq source code analysis] in depth message storage (3)
  24. Implementation of service configuration center with spring cloud + Nacos (Hoxton version)
  25. SCIP: constructing data abstraction -- Explanation of queue and tree in data structure
  26. SCIP: abstraction of construction process -- object oriented explanation
  27. Using docker to build elasticsearch + kibana cluster
  28. What are the spring IOC features? I can't understand the source code!
  29. Spring cloud upgrade road - 2020.0. X - 3. Accesslog configuration of undertow
  30. 导致Oracle性能抖动的参数提醒
  31. 风险提醒之Oracle RAC高可用失效
  32. 小机上运行Oracle需要注意的进程调度bug
  33. Oracle内存过度消耗风险提醒
  34. Oracle SQL monitor
  35. 使用Bifrost实现Mysql的数据同步
  36. 揭秘Oracle数据库truncate原理
  37. 看了此文,Oracle SQL优化文章不必再看!
  38. Mybatis (3) map and fuzzy query expansion
  39. Kafka性能篇:为何这么“快”?
  40. 两个高频设计类面试题:如何设计HashMap和线程池
  41. [TTS] AIX - & gt; Linux -- Based on RMAN (real environment)
  42. 为什么学编程大部分人选Java编程语言?
  43. Redis 高可用篇:你管这叫 Sentinel 哨兵集群原理
  44. redis 为什么把简单的字符串设计成 SDS?
  45. [TTS] transfer table space AIX - & gt; Linux based on RMAN
  46. Linux 网卡数据收发过程分析
  47. Redis 高可用篇:你管这叫 Sentinel 哨兵集群原
  48. Redis 6.X Cluster 集群搭建
  49. [TTS] transfer table space AIX ASM - & gt; Linux ASM
  50. [TTS] transfer table space Linux ASM - & gt; AIX ASM
  51. 高性能通讯框架——Netty
  52. Brief introduction and test of orchestrator, a high availability management tool for MySQL
  53. [TTS] transfer table space Linux - & gt; AIX based on RMAN
  54. A love diary about http
  55. [rocketmq source code analysis] in depth message storage (3)
  56. Implementation of service configuration center with spring cloud + Nacos (Hoxton version)
  57. SiCp: abstraction of construction process -- object oriented explanation
  58. springboot网上点餐系统
  59. 【SPM】oracle如何固定执行计划
  60. 用好HugePage,告别Linux性能故障