Backend Development 7 min read

Building a Simplified Spring Framework: Step‑by‑Step IOC, DI, and MVC Implementation

This article walks through creating a lightweight Spring‑like framework in Java, detailing the project structure, startup process, MVC module initialization, core IOC container design, BeanDefinition handling, bean registration, dependency injection, and circular‑dependency resolution, all illustrated with diagrams and code snippets.

JD Retail Technology
JD Retail Technology
JD Retail Technology
Building a Simplified Spring Framework: Step‑by‑Step IOC, DI, and MVC Implementation

The author explains why reading the full Spring source code can be overwhelming and proposes a simplified implementation that focuses on the core functionalities of IOC, DI, and MVC, providing a GitHub repository for the complete example.

The project directory is introduced, followed by a demonstration where a request to http://localhost:8082/wx/query?name=wangxin returns the expected result.

During application startup, the application.properties file loads the classpath, web.xml defines the servlet entry point, and the Jetty Maven plugin launches the server.

The MVC module initialization is described: the WXDispatcherServlet is created, its init method first sets up the IOC container and then initializes the nine core MVC components.

A concise MVC request flow is listed, from the client request reaching the DispatcherServlet to the final view rendering and response.

The core of the article focuses on IOC container initialization. The central class WXApplicationContext (analogous to Spring’s ApplicationContext ) manages bean definitions, singleton caches, and the refresh lifecycle.

private final Map<String, Object> singletonObjects //一级缓存 private final Map<String, ObjectFactory> singletonFactories //二级缓存 private final Map<String, Object> earlySingletonObjects //三级缓存

Bean definitions are scanned by WxBeanDefinitionReader , which recursively traverses packages to create a list of BeanDefinition objects. These definitions are then registered into a map for quick lookup.

The refresh method orchestrates the initialization: it invokes loadBeanDefinitions , registers them, and finally calls finishBeanFactoryInit , which iterates over each definition and invokes getBean to create instances, perform dependency injection, and apply AOP proxies.

The bean creation process includes instantiateBean (instantiating the raw object) and populateBean (injecting dependencies). Circular dependencies are resolved using the three‑level cache mechanism shown in the UML diagram.

After the finishBeanFactoryInit loop completes, the singleton cache is fully populated, meaning the IOC container and DI are operational.

In conclusion, writing a miniature Spring‑like framework is achievable; understanding the core code boosts confidence and makes reading the real Spring source much easier.

backendJavaMVCIoCSpringframeworkDI
JD Retail Technology
Written by

JD Retail Technology

Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.