# code4craft/tiny-spring architecture

> Architecture diagram of the GitHub repository code4craft/tiny-spring, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/code4craft/tiny-spring
- Repository: https://github.com/code4craft/tiny-spring

## Overview

Based on both the file tree and the README contents, this project is clearly a small, educational, and simplified implementation of the Spring IoC container with AOP support. Its primary purpose is to mimic key Spring functionalities – dependency injection (DI), bean lifecycle management, and aspect-oriented programming (AOP) – in a much leaner form for learning and experimental purposes.

##### Main Components
- Bean Configuration and XML Parsing: Represent the XML configuration file reading component. This involves the “beans/io” and “beans/xml” modules (e.g., Resource, ResourceLoader, UrlResource, and XmlBeanDefinitionReader) that read and parse XML files to produce bean definitions.
- Bean Definition and Factory: Include the “beans” and “beans/factory” packages that define BeanDefinition, AbstractBeanDefinitionReader, BeanDefinitionReader, and the BeanFactory implementations (AbstractBeanFactory, AutowireCapableBeanFactory). These form the core of the dependency injection system.
- AOP Components: Emphasize the “aop” package that contains key classes for aspect-oriented programming. These include components for AOP proxy creation (Cglib2AopProxy, JdkDynamicAopProxy, ProxyFactory), pointcut definition (AspectJExpressionPointcut), advice (AspectJAroundAdvice), and supporting classes (AdvisedSupport, AspectJAwareAdvisorAutoProxyCreator, MethodMatcher, etc.). This layer demonstrates how method interceptions and proxy generation are accomplished.
- Bean Post-Processing: Clearly mark the role of BeanPostProcessor classes that integrate with the bean creation process to allow additional processing (for example, applying AOP proxies).

##### Relationships and Data Flow
- The parsed bean definitions are passed into the BeanFactory, where they are stored and managed.
- The ApplicationContext (e.g., ClassPathXmlApplicationContext) wraps the BeanFactory and orchestrates the overall lifecycle of beans.
- During bean initialization, BeanPostProcessors and other lifecycle interceptors are applied; this is where AOP-related processing is injected.
- Optionally, include the test modules/components as the validation layer for these interactions.

##### Architectural Patterns and Design Principles
- IoC/Dependency Injection: The core mechanism for managing bean lifecycles and dependencies.
- Separation of Concerns: Notice how XML parsing, bean management, and AOP functionalities are split into distinct modules/packages.
- Proxy Pattern: Utilized in AOP to encapsulate method interception logic (e.g., using JDK or Cglib proxies).
- Observer/Listener Pattern: Implicitly used in bean post-processing and lifecycle callbacks.
- Modular layering: Dividing responsibilities among context, beans, and aop layers.

##### Relevant Technologies and Libraries
- Although this project is a simplified clone and doesn’t depend on external libraries like full Spring does, it mimics Spring’s use of dependency injection and AOP capabilities.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% XML Configuration Layer
    subgraph "XML Configuration"
        RL("Resource Loader (Resource, UrlResource)"):::xml
        XBR("XML Config Reader (XmlBeanDefinitionReader)"):::xml
    end

    %% Bean Management Layer
    subgraph "Bean Management"
        BD("BeanDefinition"):::factory
        BDR("BeanDefinitionReader"):::factory
        BF("BeanFactory (AutowireCapableBeanFactory)"):::factory
        BPP("BeanPostProcessor"):::factory
    end

    %% Application Context Layer
    subgraph "Application Context"
        CPX("ClassPathXmlApplicationContext"):::context
    end

    %% AOP Infrastructure Layer
    subgraph "AOP Infrastructure"
        PF("ProxyFactory (Cglib2AopProxy, JdkDynamicAopProxy)"):::aop
        PA("Pointcut & Advice (AspectJExpressionPointcut, AspectJAroundAdvice)"):::aop
    end

    %% Connections for XML Configuration
    RL -->|"loadsXML"| XBR

    %% Connections from XML Parsing to Bean Management
    XBR -->|"parsesXML"| BF

    %% Bean Management internal connections
    BDR -.->|"definesBeans"| BF
    BD -.->|"providesDefinition"| BF
    BF -->|"appliesPostProcessors"| BPP

    %% Application Context wraps BeanFactory
    CPX -->|"wrapsFactory"| BF

    %% AOP Integration connections
    BF -->|"checksAOP"| PF
    BPP -->|"appliesAOP"| PF
    PF -->|"delegatesTo"| PA

    %% Styles for color coding
    classDef xml fill:#F9E79F,stroke:#D35400,stroke-width:2px;
    classDef factory fill:#AED6F1,stroke:#2874A6,stroke-width:2px;
    classDef context fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;
    classDef aop fill:#A9DFBF,stroke:#27AE60,stroke-width:2px;

    %% Click Events
    click XBR "https://github.com/code4craft/tiny-spring/blob/master/src/main/java/com/ysj/tinySpring/beans/xml/XmlBeanDefinitionReader.java"
    click RL "https://github.com/code4craft/tiny-spring/tree/master/src/main/java/com/ysj/tinySpring/beans/io/"
    click BD "https://github.com/code4craft/tiny-spring/blob/master/src/main/java/com/ysj/tinySpring/beans/BeanDefinition.java"
    click BDR "https://github.com/code4craft/tiny-spring/blob/master/src/main/java/com/ysj/tinySpring/beans/BeanDefinitionReader.java"
    click BF "https://github.com/code4craft/tiny-spring/tree/master/src/main/java/com/ysj/tinySpring/beans/factory/"
    click BPP "https://github.com/code4craft/tiny-spring/blob/master/src/main/java/com/ysj/tinySpring/beans/BeanPostProcessor.java"
    click CPX "https://github.com/code4craft/tiny-spring/blob/master/src/main/java/com/ysj/tinySpring/context/ClassPathXmlApplicationContext.java"
    click PF "https://github.com/code4craft/tiny-spring/blob/master/src/main/java/com/ysj/tinySpring/aop/ProxyFactory.java"
    click PA "https://github.com/code4craft/tiny-spring/blob/master/src/main/java/com/ysj/tinySpring/aop/AspectJExpressionPointcut.java"
```
