|
1 | | -# Spring IOC, Beans, and JPA: Product Catalog System |
2 | | - |
3 | | -This repository contains a Spring Boot project that implements a Product Catalog System using Spring IOC, Beans, and Spring Data JPA. The project allows users to add products to a catalog and display products by category in a tabular format on a web page. |
4 | | - |
5 | | -## Project Structure |
6 | | - |
7 | | -- **src/main/java/com/example/demo/** |
8 | | - - **entity/Product.java**: The JPA entity class for the Product with attributes `name`, `price`, and `category`. |
9 | | - - **repository/ProductRepository.java**: Spring Data JPA repository interface with a method to fetch products by category. |
10 | | - - **service/ProductService.java**: Service class to handle business logic for adding and retrieving products. |
11 | | - - **controller/ProductController.java**: Controller class to handle HTTP requests and render HTML pages. |
12 | | -- **src/main/resources/templates/** |
13 | | - - **index.html**: The homepage with options to "Add Product" or "Display Product". |
14 | | - - **add-product.html**: HTML form to add a new product (name, price, category). |
15 | | - - **display-products.html**: HTML page to display all products in a tabular format. |
16 | | -- **src/main/resources/application.properties**: Configuration file for the database (e.g., H2 in-memory database). |
17 | | -- **pom.xml**: Maven configuration file with dependencies for Spring Boot, Spring Data JPA, Thymeleaf, and H2 database. |
18 | | -- **screenshots/**: Contains screenshots of the project (e.g., homepage.png, add-product.png, display-products.png). |
19 | | - |
20 | | -## Task Description |
21 | | - |
22 | | -### Q1: Product Catalog System |
23 | | -This task involves creating a web-based Product Catalog System with the following features: |
24 | | -- **Homepage**: |
25 | | - - A RESTful endpoint (`/`) displays an HTML page with two options: |
26 | | - 1. Add Product |
27 | | - 2. Display Product |
28 | | -- **Add Product**: |
29 | | - - Clicking "Add Product" navigates to `/add-product`, displaying a form to input: |
30 | | - - Product Name |
31 | | - - Price |
32 | | - - Category |
33 | | - - Submitting the form saves the product to the database using Spring Data JPA. |
34 | | -- **Display Product**: |
35 | | - - Clicking "Display Product" navigates to `/display-products`, showing all products in a table. |
36 | | - - Includes a filter to fetch products by a specific category using a repository method. |
37 | | -- **Backend**: |
38 | | - - Uses Spring Data JPA to create an entity (`Product`), repository (`ProductRepository`), and service (`ProductService`). |
39 | | - - Stores product information in a database (H2 in-memory database by default). |
40 | | - |
41 | | -## How to Run |
42 | | - |
43 | | -1. **Clone the Repository**: |
44 | | - ```bash |
45 | | - git clone https://github.com/thesoulseizure/Spring-IOC-Beans.git |
46 | | - ``` |
47 | | -2. **Navigate to the Project Directory**: |
48 | | - ```bash |
49 | | - cd Spring-IOC-Beans |
50 | | - ``` |
51 | | -3. **Build the Project**: |
52 | | - - Ensure you have Maven installed. |
53 | | - ```bash |
54 | | - mvn clean install |
55 | | - ``` |
56 | | -4. **Run the Application**: |
57 | | - ```bash |
58 | | - mvn spring-boot:run |
59 | | - ``` |
60 | | -5. **Access the Application**: |
61 | | - - Open a browser and navigate to `http://localhost:8080`. |
62 | | - - The homepage will display options to add or display products. |
63 | | - |
64 | | -## Requirements |
65 | | - |
66 | | -- **Java**: JDK 17 or higher. |
67 | | -- **Maven**: For dependency management and building the project. |
68 | | -- **Database**: H2 in-memory database (configured by default; can be changed in `application.properties`). |
69 | | -- **Browser**: To access the web application. |
70 | | - |
71 | | -## Screenshots |
72 | | - |
73 | | -The repository includes screenshots in the `screenshots/` directory: |
74 | | -- `homepage.png`: Shows the homepage with "Add Product" and "Display Product" options. |
75 | | -- `add-product.png`: Displays the form to add a new product. |
76 | | -- `display-products.png`: Shows the product catalog in a tabular format. |
77 | | - |
78 | | -## Technologies Used |
79 | | - |
80 | | -- **Spring Boot**: For creating the web application. |
81 | | -- **Spring IOC and Beans**: For dependency injection and bean management. |
82 | | -- **Spring Data JPA**: For database operations. |
83 | | -- **Thymeleaf**: For server-side rendering of HTML templates. |
84 | | -- **H2 Database**: In-memory database for development. |
85 | | -- **Maven**: For project build and dependency management. |
| 1 | +# 🚀 Spring IOC, Beans & JPA — Product Catalog System |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +A complete, production‑style demonstration of **Spring IOC**, **Beans**, **Dependency Injection**, **Spring MVC**, **Spring Data JPA**, and **Thymeleaf**, wrapped into a functional **Product Catalog Web Application**. |
| 13 | + |
| 14 | +This project showcases real‑world Spring Framework fundamentals with a clean architecture and fully working CRUD features. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +# 📌 Table of Contents |
| 19 | +- [Overview](#overview) |
| 20 | +- [Features](#features) |
| 21 | +- [System Architecture](#system-architecture) |
| 22 | +- [Flow Diagram](#flow-diagram) |
| 23 | +- [Tech Stack](#tech-stack) |
| 24 | +- [Project Structure](#project-structure) |
| 25 | +- [Endpoints & Views](#endpoints--views) |
| 26 | +- [Entity Model](#entity-model) |
| 27 | +- [How to Run](#how-to-run) |
| 28 | +- [H2 Database Access](#h2-database-access) |
| 29 | +- [Troubleshooting](#troubleshooting) |
| 30 | +- [License](#license) |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +# 📖 Overview |
| 35 | +This **Spring Boot + IOC + Beans** powered application provides a simple yet powerful **Product Catalog** with full CRUD behavior through: |
| 36 | + |
| 37 | +- Thymeleaf‑based UI |
| 38 | +- Spring MVC controllers |
| 39 | +- JPA repositories |
| 40 | +- H2 in-memory DB |
| 41 | +- IOC + DI-managed services |
| 42 | + |
| 43 | +It is ideal for learning or demonstrating **Spring Core concepts**. |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +# 🌟 Features |
| 48 | + |
| 49 | +### ✔ Dependency Injection & IoC |
| 50 | +Services and controllers are wired automatically using Spring Beans. |
| 51 | + |
| 52 | +### ✔ Add Products |
| 53 | +Fill out a form and store product data in H2 DB via JPA. |
| 54 | + |
| 55 | +### ✔ Display Products |
| 56 | +See all products in a table with sorting and clean UI. |
| 57 | + |
| 58 | +### ✔ Filter by Category |
| 59 | +Uses repository-level custom query. |
| 60 | + |
| 61 | +### ✔ Thymeleaf UI |
| 62 | +Server-side rendered dynamic HTML pages. |
| 63 | + |
| 64 | +### ✔ In-Memory H2 Database |
| 65 | +Zero configuration, auto‑created schema. |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +# 🏛 System Architecture |
| 70 | + |
| 71 | +```mermaid |
| 72 | +graph TD |
| 73 | + A[User Browser] --> B[Spring MVC Controller] |
| 74 | + B --> C[Service Layer] |
| 75 | + C --> D[Repository Layer] |
| 76 | + D --> E[(H2 Database)] |
| 77 | + B --> F[Thymeleaf Templates] |
| 78 | +``` |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +# 🔄 Flow Diagram (Page → Controller → Service → DB) |
| 83 | + |
| 84 | +```mermaid |
| 85 | +sequenceDiagram |
| 86 | + participant U as User |
| 87 | + participant C as Controller |
| 88 | + participant S as Service |
| 89 | + participant R as JPA Repository |
| 90 | + participant DB as H2 DB |
| 91 | +
|
| 92 | + U->>C: GET / |
| 93 | + C->>U: index.html |
| 94 | +
|
| 95 | + U->>C: GET /add-product |
| 96 | + C->>U: add-product.html |
| 97 | +
|
| 98 | + U->>C: POST /add-product |
| 99 | + C->>S: saveProduct(product) |
| 100 | + S->>R: save(product) |
| 101 | + R->>DB: INSERT |
| 102 | + C->>U: redirect to / |
| 103 | +
|
| 104 | + U->>C: GET /display-products?category=... |
| 105 | + C->>S: getProducts(...) |
| 106 | + S->>R: findByCategory() OR findAll() |
| 107 | + R->>DB: SELECT |
| 108 | + C->>U: display-products.html |
| 109 | +``` |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +# 🧰 Tech Stack |
| 114 | + |
| 115 | +| Component | Technology | |
| 116 | +|----------|------------| |
| 117 | +| Language | Java 17 | |
| 118 | +| Framework | Spring Boot, Spring Core | |
| 119 | +| DI / IoC | Spring Beans | |
| 120 | +| Persistence | Spring Data JPA (Hibernate) | |
| 121 | +| Database | H2 In‑Memory | |
| 122 | +| Template Engine | Thymeleaf | |
| 123 | +| Build Tool | Maven | |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +# 📂 Project Structure |
| 128 | + |
| 129 | +``` |
| 130 | +src/ |
| 131 | + ├── main/ |
| 132 | + │ ├── java/com/SpringIOC/demo/ |
| 133 | + │ │ ├── controller/ |
| 134 | + │ │ │ └── ProductController.java |
| 135 | + │ │ ├── service/ |
| 136 | + │ │ │ └── ProductService.java |
| 137 | + │ │ ├── repository/ |
| 138 | + │ │ │ └── ProductRepository.java |
| 139 | + │ │ ├── entity/ |
| 140 | + │ │ │ └── Product.java |
| 141 | + │ │ └── SpringIoc1Application.java |
| 142 | + │ │ |
| 143 | + │ └── resources/ |
| 144 | + │ ├── templates/ |
| 145 | + │ │ ├── index.html |
| 146 | + │ │ ├── add-product.html |
| 147 | + │ │ └── display-products.html |
| 148 | + │ ├── application.properties |
| 149 | + │ └── static/ |
| 150 | + │ |
| 151 | + └── test/ |
| 152 | + └── SpringIoc1ApplicationTests.java |
| 153 | +``` |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +# 🔗 Endpoints & Views |
| 158 | + |
| 159 | +| URL | Method | Description | View | |
| 160 | +|-----|--------|-------------|-------| |
| 161 | +| `/` | GET | Homepage | index.html | |
| 162 | +| `/add-product` | GET | Show product form | add-product.html | |
| 163 | +| `/add-product` | POST | Save new product | redirect:/ | |
| 164 | +| `/display-products` | GET | Display all or filtered by category | display-products.html | |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +# 🗄 Entity Model |
| 169 | + |
| 170 | +### **Product.java** |
| 171 | +``` |
| 172 | +id (Long, Auto-generated) |
| 173 | +name (String) |
| 174 | +price (Double) |
| 175 | +category (String) |
| 176 | +``` |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +# ▶️ How to Run |
| 181 | + |
| 182 | +### **1. Clone the Repo** |
| 183 | +```bash |
| 184 | +git clone <REPO_URL> |
| 185 | +cd SpringIOC |
| 186 | +``` |
| 187 | + |
| 188 | +### **2. Build the Project** |
| 189 | +```bash |
| 190 | +mvn clean install |
| 191 | +``` |
| 192 | + |
| 193 | +### **3. Run** |
| 194 | +```bash |
| 195 | +mvn spring-boot:run |
| 196 | +``` |
| 197 | + |
| 198 | +### **4. Access in Browser** |
| 199 | +``` |
| 200 | +http://localhost:8080/ |
| 201 | +``` |
| 202 | + |
| 203 | +--- |
| 204 | + |
| 205 | +# 🗃 H2 Database Access |
| 206 | + |
| 207 | +Visit: |
| 208 | +``` |
| 209 | +http://localhost:8080/h2-console |
| 210 | +``` |
| 211 | + |
| 212 | +Use: |
| 213 | + |
| 214 | +| Key | Value | |
| 215 | +|-----|-------| |
| 216 | +| JDBC URL | jdbc:h2:mem:productdb | |
| 217 | +| Username | sa | |
| 218 | +| Password | *(empty)* | |
| 219 | + |
| 220 | +--- |
| 221 | + |
| 222 | +# 🛠 Troubleshooting |
| 223 | + |
| 224 | +### ⚠ Whitelabel Error |
| 225 | +Caused by template naming mismatch. |
| 226 | +Ensure: |
| 227 | +``` |
| 228 | +add-product.html == return "add-product" |
| 229 | +display-products.html == return "display-products" |
| 230 | +``` |
| 231 | + |
| 232 | +### ⚠ H2 Shutdown Error |
| 233 | +Fixed by: |
| 234 | +``` |
| 235 | +spring.datasource.url=jdbc:h2:mem:productdb;DB_CLOSE_ON_EXIT=FALSE |
| 236 | +``` |
| 237 | + |
| 238 | +--- |
| 239 | + |
| 240 | +# 📄 License |
| 241 | +MIT License. |
| 242 | + |
| 243 | +--- |
| 244 | + |
| 245 | +✨ *This README is crafted to be professional, recruiter‑friendly, and portfolio‑ready — showcasing your Spring IOC, DI, MVC, JPA, and Thymeleaf skills in the best possible way.* |
0 commit comments