# outapython/spring-mvc architecture

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

- Interactive diagram: https://gitdiagram.com/outapython/spring-mvc
- Repository: https://github.com/outapython/spring-mvc

## Overview

##### Project type & purpose (what you’re diagramming)
This repository is a **monolithic Spring Boot MVC web application** (server-rendered UI) for **Product Management**:
- UI is rendered on the server using **Thymeleaf templates**.
- Business flow is classic **Spring MVC**: Controller → Repository (Spring Data JPA) → Database.
- Access is gated by **Spring Security** with role-based authorization (`USER`, `ADMIN`) + CSRF.
- Persistence uses **JPA** with either **H2** (runtime/dev) or **MySQL** (runtime) configured via `application.properties`.

- Request/response paths (browser ↔ Spring MVC)
- Security filter chain placement
- Data access via repository/JPA to DB
- Server-side view rendering pipeline

##### Architecture inferred from file tree (how it’s structured)
Key components visible in the tree:

##### Application entrypoint
- `BdccSpringEnsetMvcApplication.java`
Bootstraps Spring context (auto-config), starts embedded web server (Tomcat by default).

##### Domain model
- `entities/Product.java`
JPA entity representing products.

##### Data access layer
- `repository/ProductRepository.java`
Spring Data JPA repository interface; generates CRUD/search queries.

##### Web layer
- `web/ProductController.java`
Handles HTTP routes for list/search/create/edit/delete; binds forms, validation, returns Thymeleaf views.

##### Security layer
- `security/SecurityConfig.java`
Defines authentication/authorization rules, login page, CSRF, role restrictions.

##### Views / UI (server-rendered)
- `resources/templates/*.html`
`layout1.html` (base layout), `products.html`, `newProduct.html`, `editProduct.html`, `login.html`, `notAuthorized.html`.

##### Configuration
- `resources/application.properties`
Server port (8094 per README), DB config (H2/MySQL), etc.

##### External actor
- **End User (Browser)**

##### Runtime container
- **Spring Boot Application (Embedded Tomcat / HTTP Server)**

Inside the Spring Boot app, show separate components:
- **Spring Security Filter Chain**
- **Spring MVC DispatcherServlet**
- **ProductController (Web Layer)**
- **Validation (Bean Validation / Hibernate Validator)** (since README mentions form validation)
- **Thymeleaf View Engine**
- **ProductRepository (Data Access)**
- **JPA / Hibernate (ORM Provider)**
- **Database**: H2 (dev) or MySQL (prod-like)

Optional but useful to call out:

##### Thymeleaf → Browser returns HTML

On login submit:
##### Browser → POST /login (form submit)
2. **Spring Security Filter Chain** authenticates user (in-memory or configured user store; README indicates default users)

##### Mutations: create/edit/delete (ADMIN-only typically)
Same as above, but include:
- **Validation**: Controller binds form → validates → returns same form page with errors if invalid.
- **Repository.save / deleteById** operations to DB.
- **CSRF**: show that POST/PUT/DELETE form submits include CSRF token enforced by Spring Security.

##### Important architectural patterns to highlight (specific to this repo)
- **Layered MVC**:
- Presentation: Thymeleaf templates
- Controller: `ProductController`
- Persistence: `ProductRepository`
- Domain: `Product` entity
- **Spring Security “cross-cutting”**:
- Security sits in front of MVC handling as a filter chain.
- **Repository pattern via Spring Data JPA**:
- Interface-driven repository, runtime implementation generated by Spring.

Purpose: Show what the system is and who uses it.
- Person: “User/Admin”
- System: “Spring Boot Product Management App”
- External dependency: “Database (H2/MySQL)”
Optional: “Browser” as the client.

Purpose: Show the internals clearly.
- SecurityConfig / Spring Security Filter Chain
- DispatcherServlet
- ProductController
- Thymeleaf View Engine + Templates
- ProductRepository
- JPA/Hibernate
- DB

- Auth requests (/login)
- Product CRUD routes (/products, /add, /edit, /delete)
- DB interactions (read/write)

- Login (POST /login)
- Create Product (GET form, POST form with CSRF, validation, save)

##### Minimal component inventory to ensure you “separate into as many components as possible”
- `BdccSpringEnsetMvcApplication` (Bootstrapping)
- `SecurityConfig` (authz/authn rules, CSRF)
- `ProductController` (CRUD routes, validation binding)
- `Product` entity
- `ProductRepository`
- Thymeleaf templates: `layout1`, `products`, `newProduct`, `editProduct`, `login`, `notAuthorized`
- DB: H2 / MySQL

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% =======================
  %% Diagram 1 — System Context (C4 L2)
  %% =======================
  subgraph "Diagram 1 — System Context (C4 L2)"
    direction TB

    U["User/Admin"]:::actor
    B["Web Browser<br/>http://localhost:8094"]:::web

    subgraph "Spring Boot Product Management App"
      direction TB
      APP["Monolithic Spring Boot MVC App<br/>(Embedded Tomcat + Spring MVC + Thymeleaf + Security)"]:::infra
    end

    DB["Database<br/>H2 (dev/runtime) or MySQL (runtime)"]:::db
    ASSETS["UI Assets<br/>(Bootstrap/WebJars)"]:::web

    U -->|"uses"| B
    B -->|"HTTP(S) requests"| APP
    APP -->|"JPA read/write"| DB
    APP -->|"serves static assets"| ASSETS
  end

  %% =======================
  %% Diagram 2 — Container/Component View (C4 L3-ish)
  %% =======================
  subgraph "Diagram 2 — Component View (inside the monolith)"
    direction TB

    BR["Browser"]:::web

    subgraph "Spring Boot Runtime (Embedded Tomcat)"
      direction TB

      ENTRY["BdccSpringEnsetMvcApplication<br/>(Bootstraps Spring context)"]:::infra

      SEC["Spring Security Filter Chain<br/>(AuthN/AuthZ, CSRF, Roles: USER/ADMIN)"]:::security
      CFG["SecurityConfig<br/>(Security rules)"]:::security

      MVC["DispatcherServlet<br/>(Spring MVC)"]:::infra

      subgraph "Web Layer"
        direction TB
        CTRL["ProductController<br/>(CRUD routes + form binding)"]:::logic
        VAL["Bean Validation<br/>(Hibernate Validator)"]:::logic
      end

      subgraph "View Rendering"
        direction TB
        THY["Thymeleaf View Engine"]:::web
        subgraph "Templates (*.html)"
          direction TB
          TDIR["templates/"]:::web
          LAYOUT["layout1.html"]:::web
          TPROD["products.html"]:::web
          TNEW["newProduct.html"]:::web
          TEDIT["editProduct.html"]:::web
          TLOGIN["login.html"]:::web
          TNA["notAuthorized.html"]:::web
        end
      end

      subgraph "Persistence Layer"
        direction TB
        REPO["ProductRepository<br/>(Spring Data JPA)"]:::data
        ORM["JPA/Hibernate ORM"]:::data
        ENTITY["Product<br/>(JPA Entity)"]:::data
      end

      PROPS["application.properties<br/>(port 8094, DB config H2/MySQL)"]:::infra
    end

    DB2["H2 / MySQL"]:::db

    %% --- Request flows (vertically oriented) ---
    BR -->|"HTTP GET /login"| SEC
    SEC -->|"allowAnonymous"| MVC
    MVC -->|"route"| THY
    THY -->|"render login.html"| TLOGIN
    THY -.->|"HTML response"| BR

    BR -->|"HTTP POST /login"| SEC
    SEC -->|"authenticate"| MVC
    MVC -->|"onSuccess redirect"| BR

    BR -->|"HTTP GET /products?keyword=..."| SEC
    SEC -->|"checkAuth (USER/ADMIN)"| MVC
    MVC -->|"dispatch"| CTRL
    CTRL -->|"query (findAll/findByNameContains)"| REPO
    REPO -->|"JPA query"| ORM
    ORM -->|"SQL"| DB2
    DB2 -->|"rows"| ORM
    ORM -->|"entities"| REPO
    REPO -->|"Product list"| CTRL
    CTRL -->|"Model attributes"| THY
    THY -->|"render products.html"| TPROD
    THY -.->|"HTML response"| BR

    BR -->|"HTTP GET /products/new"| SEC
    SEC -->|"ADMIN only"| MVC
    MVC -->|"dispatch"| CTRL
    CTRL -->|"render form"| THY
    THY -->|"render newProduct.html"| TNEW
    THY -.->|"HTML response"| BR

    BR -->|"HTTP POST /products (CSRF-protected)"| SEC
    SEC -->|"enforceCSRF + ADMIN"| MVC
    MVC -->|"dispatch"| CTRL
    CTRL -->|"bind+validate"| VAL
    VAL -->|"ok"| CTRL
    CTRL -->|"save"| REPO
    REPO -->|"JPA persist"| ORM
    ORM -->|"SQL write"| DB2
    CTRL -->|"redirect /products"| BR

    BR -->|"HTTP GET /products/edit?id=..."| SEC
    SEC -->|"ADMIN only"| MVC
    MVC -->|"dispatch"| CTRL
    CTRL -->|"loadById"| REPO
    REPO -->|"JPA query"| ORM
    ORM -->|"SQL"| DB2
    CTRL -->|"render edit form"| THY
    THY -->|"render editProduct.html"| TEDIT
    THY -.->|"HTML response"| BR

    BR -->|"HTTP POST /products/delete?id=... (CSRF)"| SEC
    SEC -->|"enforceCSRF + ADMIN"| MVC
    MVC -->|"dispatch"| CTRL
    CTRL -->|"deleteById"| REPO
    REPO -->|"JPA delete"| ORM
    ORM -->|"SQL write"| DB2
    CTRL -->|"redirect /products"| BR

    SEC -->|"onDenied"| THY
    THY -->|"render notAuthorized.html"| TNA
  end

  %% =======================
  %% Diagram 3 — Request Sequence (Login + Create Product)
  %% =======================
  subgraph "Diagram 3 — Sequence (Login + Create Product)"
    direction TB

    subgraph "Sequence: Login"
      direction TB
      S1["Browser: POST /login"]:::web
      S2["Security Filter Chain: authenticate"]:::security
      S3["Redirect: /products"]:::web
      S1 --> S2 --> S3
    end

    subgraph "Sequence: Create Product (ADMIN)"
      direction TB
      C1["Browser: GET /products/new"]:::web
      C2["Security: ADMIN check"]:::security
      C3["DispatcherServlet -> ProductController"]:::infra
      C4["Thymeleaf: render newProduct.html"]:::web
      C5["Browser: POST /products (CSRF token)"]:::web
      C6["Security: CSRF + ADMIN check"]:::security
      C7["Controller: bind + validate"]:::logic
      C8["Repository.save -> JPA/Hibernate -> DB"]:::data
      C9["Redirect: /products"]:::web
      C1 --> C2 --> C3 --> C4 --> C5 --> C6 --> C7 --> C8 --> C9
    end
  end

  %% =======================
  %% Click Events (from component_mapping)
  %% =======================
  click ENTRY "https://github.com/outapython/spring-mvc/blob/master/src/main/java/ma/enset/bdccspringensetmvc/BdccSpringEnsetMvcApplication.java"
  click ENTITY "https://github.com/outapython/spring-mvc/blob/master/src/main/java/ma/enset/bdccspringensetmvc/entities/Product.java"
  click REPO "https://github.com/outapython/spring-mvc/blob/master/src/main/java/ma/enset/bdccspringensetmvc/repository/ProductRepository.java"
  click CTRL "https://github.com/outapython/spring-mvc/blob/master/src/main/java/ma/enset/bdccspringensetmvc/web/ProductController.java"
  click CFG "https://github.com/outapython/spring-mvc/blob/master/src/main/java/ma/enset/bdccspringensetmvc/security/SecurityConfig.java"
  click PROPS "https://github.com/outapython/spring-mvc/blob/master/src/main/resources/application.properties"
  click TDIR "https://github.com/outapython/spring-mvc/tree/master/src/main/resources/templates/"
  click LAYOUT "https://github.com/outapython/spring-mvc/blob/master/src/main/resources/templates/layout1.html"
  click TPROD "https://github.com/outapython/spring-mvc/blob/master/src/main/resources/templates/products.html"
  click TNEW "https://github.com/outapython/spring-mvc/blob/master/src/main/resources/templates/newProduct.html"
  click TEDIT "https://github.com/outapython/spring-mvc/blob/master/src/main/resources/templates/editProduct.html"
  click TLOGIN "https://github.com/outapython/spring-mvc/blob/master/src/main/resources/templates/login.html"
  click TNA "https://github.com/outapython/spring-mvc/blob/master/src/main/resources/templates/notAuthorized.html"

  %% =======================
  %% Styles (color-coded)
  %% =======================
  classDef actor fill:#ffffff,stroke:#111827,stroke-width:2px,color:#111827
  classDef web fill:#dbeafe,stroke:#1d4ed8,stroke-width:1.5px,color:#0b1220
  classDef security fill:#ffedd5,stroke:#c2410c,stroke-width:1.5px,color:#0b1220
  classDef logic fill:#dcfce7,stroke:#15803d,stroke-width:1.5px,color:#0b1220
  classDef data fill:#f3e8ff,stroke:#7c3aed,stroke-width:1.5px,color:#0b1220
  classDef infra fill:#e5e7eb,stroke:#374151,stroke-width:1.5px,color:#0b1220
  classDef db fill:#f9fafb,stroke:#111827,stroke-width:2px,color:#0b1220
```
