# simple-framework **Repository Path**: Enndfp/simple-framework ## Basic Information - **Project Name**: simple-framework - **Description**: SimpleFramework 是一个简易版的 Spring 框架,专注于实现 Spring 的三大核心功能:IOC(控制反转)、AOP(面向切面编程)和 MVC(模型-视图-控制器)。该项目旨在通过重新构建这些关键功能,为开发者提供深入理解 Spring 框架架构和设计理念的机会,同时也是一种提升 Java 开发技术能力的实践。 - **Primary Language**: Java - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 41 - **Forks**: 0 - **Created**: 2024-01-22 - **Last Updated**: 2025-11-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [简体中文](README.md) | **English**

🌟 SimpleFramework

🛠️ Wheel-making project: Implementing the Spring framework from scratch
## 📖 Project Description > The Spring framework occupies a pivotal position in the Java development world, mainly due to its easy-to-understand and powerful features. It widely applies a variety of design patterns to provide a standardized architecture for the project. More importantly, Spring, as an open source framework, provides developers with opportunities to learn and improve, and brings an innovative spring breeze to Java development. > > In view of these advantages of Spring, many Java developers are eager to use basic technologies to implement a framework similar to Spring. This approach is not only an in-depth understanding of Spring architecture and design concepts, but also a demonstration of technical capabilities. Therefore, the "simple-framework" project came into being, aiming to implement a simplified version of the Spring framework so that developers can more easily understand its core concepts and at the same time improve their technical level in the field of Java development. **Simple-Framework is a free open source project that provides an easy-to-use and learn Java development framework for all individuals and enterprises, supporting the common progress and innovation of the developer community. ** ## 🚀 Technical Highlights - **Java 1.8**: Provides optimized performance and stability and is the cornerstone of Java development. - **CGLIB 3.3.0 & AspectJWeaver 1.9.5**: Powerful libraries that provide a solid foundation for AOP. - **Java Servlet API & Gson & Lombok**: These technologies together form a powerful web application development environment. ## 📚 Project architecture diagram ### 🔄 IOC ![image-20240118205412786](https://img.enndfp.cn/202401182054883.png) ### 🔀 AOP ![image-20240119133233090](https://img.enndfp.cn/202401191332233.png) ### 🕸️ MVC ![image-20240119142136722](https://img.enndfp.cn/202401191421810.png) ## ✨ The main function This project is a **simple version of the Spring Framework** that implements the three core functions of the Spring Framework: **IOC** (Inversion of Control), **AOP** (Aspect-Oriented Programming) and **MVC* * (Model-View-Controller) and split it into the following core packages: #### 📦 Core package **Function**: The Core package implements the **core functions** of the framework, including **Bean scanning and loading**, **container maintenance**, **single case mode implementation**, and** Handling of custom beans**. **Implementation method**: Use **Java reflection mechanism** to dynamically scan and load classes under specified packages, identify and process different types of annotations (such as `@Component`, `@Controller`, etc.) to manage different types of Beans. At the same time, it implements the **single case mode**, ensuring that each Bean is instantiated only once, and provides basic methods for operating Beans, such as adding, obtaining, and managing Bean instances. #### 💉 Inject package **Function**: The Inject package is responsible for **dependency injection**, including processing `@Autowired` annotations, implementing **dependency injection in singleton mode**, and injecting implementation classes for **interfaces**. **Implementation method**: Scan the fields of the Bean through the **Java reflection mechanism** to find the fields with the `@Autowired` annotation, and use the Bean container to obtain and inject the required dependencies. It supports dependency injection in singleton mode to ensure the consistency and uniqueness of dependencies. At the same time, it can also dynamically inject appropriate implementation classes into the interface, improving the flexibility and maintainability of the code. #### 🔍 AOP package **Function**: The AOP package follows the **aspect-oriented programming idea**, uses `Aspect` and `Order` annotations to identify and sort aspect classes, and implements horizontal aspects through **CGlib dynamic proxy** and **AspectJWeaver** Weave in all logic and dynamically modify method logic. **Implementation method**: Use **CGlib** to create a proxy of the target class, and intercept method calls by implementing the `MethodInterceptor` interface. This allows aspect logic (such as logging, permission checks, etc.) to be performed before and after method execution. At the same time, the expression language of **AspectJ** provides more precise control over the proxy class, allowing the method logic to be modified and enhanced according to different needs. #### 🌐 MVC package **Function**: MVC package handles **request distribution related functions**, including reconstructing `DispatcherServlet`, implementing `RequestProcessorChain` and `RequestProcessor` matrices, and `ResultRender` matrix to complete various request processing and response rendering . **Implementation**: Use `DispatcherServlet` as the **central controller** to process all HTTP requests and distribute them to the corresponding processors. Use `RequestProcessorChain` to manage and execute a series of request processors to handle different types of requests (such as static resources, controller methods, etc.). The `ResultRender` matrix is responsible for selecting an appropriate rendering strategy based on the processing results, such as rendering an HTML page or returning JSON data, ensuring that the response is correctly rendered and returned to the client. ## 💡 Quick start guide To get started using **SimpleFramework**, you can take the following steps: ### 📥 Method 1: Use source code 1. Clone the repository: ```bash git clone https://github.com/Enndfp/simple-framework.git ``` 2. Import the project into your IDE (eg IntelliJ IDEA). 3. Perform relevant tests in the `demo` directory. This is similar to developing projects with Spring Boot. ### 📦 Method 2: War package deployment 1. Build the project and generate the War package. 2. Deploy the War package to your Servlet container, such as Apache Tomcat. 3. Start the container and the application will be deployed automatically. ### 🌟 Sample code Here is a simple example showing how to use **SimpleFramework** in your project: ```java import com.simpleframework.core.BeanContainer; public class MyApplication { public static void main(String[] args) { //Initialize container BeanContainer container = BeanContainer.getInstance(); container.loadBeans("com.yourpackage"); // Use the container to get the Bean MyService myService = (MyService) container.getBean(MyService.class); myService.doSomething(); } } ``` In this example, we first obtain an instance of `BeanContainer` and then load all beans under the specified package path. After that, we get an instance of the `MyService` class from the container and call its methods.