https://github.com/hasinghgrid/applicationcontexttask
https://github.com/hasinghgrid/applicationcontexttask
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/hasinghgrid/applicationcontexttask
- Owner: hasinghgrid
- Created: 2025-06-13T05:16:43.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-06-13T08:19:00.000Z (12 months ago)
- Last Synced: 2025-07-03T23:43:09.965Z (11 months ago)
- Language: Java
- Size: 4.89 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# ApplicationContextTask - Spring Boot with Profiles
This module demonstrates how to configure and run a Spring Boot application for a **Cache Eviction Policy System** using **Spring Profiles** to separate environments and **ApplicationContext** features.
## Overview
The goal of this module is to:
* Convert a standard Java application to a Spring Boot-based application
* Use two Spring Profiles: `dev` and `prod`
* Set up different data sources based on active profiles
* Maintain flexibility to switch between embedded (H2) and external (JDBC) databases
---
## ๐ง Technologies Used
* Java 17+
* Spring Boot 3+
* Spring Context
* Spring JDBC
* Maven
* H2 (for development)
* PostgreSQL/MySQL (via JDBC in prod)
---
## ๐ Running the Application
### 1. Development Profile (`dev`) - H2 Embedded
Use this profile for local development with an in-memory H2 database.
```bash
SPRING_PROFILES_ACTIVE=dev mvn spring-boot:run
```
#### `application-dev.properties`
```properties
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:cachedb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.platform=h2
```
### 2. Production Profile (`prod`) - External JDBC DB
Use this profile when connecting to a production database (e.g., PostgreSQL or MySQL).
```bash
SPRING_PROFILES_ACTIVE=prod mvn spring-boot:run
```
#### `application-prod.properties`
```properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/cachedb
spring.datasource.username=postgres
spring.datasource.password=secret
```
---
## โ
Features
* Switch between `dev` (H2) and `prod` (PostgreSQL/MySQL) using profiles
* Loads the appropriate configuration using `@Profile`
* Leverages `ApplicationContext` to initialize and test services
* JDBC-only based implementation (No JPA)
---
## ๐งช Testing
* Run with both profiles and verify correct DB connection and data
* Observe logs for profile-specific beans loaded
---
## ๐ Notes
* JDBC implementation remains the same and is reused
* All beans like `CacheDAO`, `CacheService` are initialized via Spring context
* `ApplicationContextTask` is now Spring Boot aware
---