2026-02-08 18:02:36 +05:30
# Feature Flag System - Spring PetClinic Integration
2013-09-27 11:19:25 +08:00
2026-02-08 18:02:36 +05:30
## Overview
This project adds a custom-built Feature Flag system to the Spring PetClinic application without using any third‑ party feature flag libraries. The system allows runtime control of selected features using database-driven flags and an AOP-based custom annotation.
2023-03-04 14:45:18 +01:00
2013-03-20 16:13:31 +08:00
2026-02-08 18:02:36 +05:30
Features can be enabled/disabled globally and also support whitelist, blacklist, and percentage rollout strategies.
2025-09-16 17:12:38 +05:30
2026-02-08 18:02:36 +05:30
---
2025-09-16 17:12:38 +05:30
2026-02-08 18:02:36 +05:30
## Tech Stack
2018-10-08 09:05:39 +01:00
2026-02-08 18:02:36 +05:30
- Java 17+
- Spring Boot 4.0.1 (Default version of Spring PetClinic)
- Spring AOP
- Spring Data JPA
- MySQL
- Maven
---
## How To Run Locally
### Clone Repo
2026-01-02 08:11:31 +01:00
You first need to clone the project locally:
2023-11-24 19:21:08 +01:00
```bash
2018-10-08 21:50:50 +02:00
git clone https://github.com/spring-projects/spring-petclinic.git
cd spring-petclinic
2013-02-21 10:10:41 +08:00
```
2026-02-08 18:02:36 +05:30
## Database configuration
In its default configuration, Petclinic uses an in-memory database (H2) which
gets populated at startup with data.
A similar setup is provided for MySQL and PostgreSQL if a persistent database configuration is needed. Note that whenever the database type changes, the app needs to run with a different profile: `spring.profiles.active=mysql` for MySQL or `spring.profiles.active=postgres` for PostgreSQL. See the [Spring Boot documentation ](https://docs.spring.io/spring-boot/how-to/properties-and-configuration.html#howto.properties-and-configuration.set-active-spring-profiles ) for more detail on how to set the active profile.
I have used MySQL 8.0.34 for development and testing.
For which I have added the following configuration to `src/main/resources/application-mysql.properties` :
```properties
spring.jpa.hibernate.ddl-auto=update
```
Before running the application with MySQL, make sure to create a database named `petclinic` if required.
### Build & Run
You can start the application using maven on the command-line as follows:
2018-10-08 09:05:39 +01:00
2023-11-24 19:21:08 +01:00
```bash
2018-10-08 21:50:50 +02:00
./mvnw spring-boot:run
2018-10-08 09:05:39 +01:00
```
2026-02-08 18:02:36 +05:30
If you are using MySQL, you can start the application with the MySQL profile:
2026-01-02 08:11:31 +01:00
```bash
2026-02-08 18:02:36 +05:30
./mvnw spring-boot:run -Dspring-boot.run.profiles=mysql
2026-01-02 08:11:31 +01:00
```
You can then access the Petclinic at < http: / / localhost:8080 / > .
2026-01-01 16:54:45 +05:30
2026-02-08 18:02:36 +05:30
---
2026-01-01 16:54:45 +05:30
2021-12-16 11:46:05 +00:00
2026-02-08 18:02:36 +05:30
## Feature Flags Implemented
2022-01-10 10:46:26 +00:00
2026-02-08 18:02:36 +05:30
| Feature | Flag Name | Location |
|--------------|--------------|------------------------------------------|
| Add Pet | ADD_PET | PetController POST processCreationForm |
| Add Visit | ADD_VISIT | VisitController POST processNewVisitForm |
| Owner Search | OWNER_SEARCH | OwnerController GET processFindForm |
2022-01-10 10:46:26 +00:00
2013-05-07 10:58:21 +08:00
2026-02-08 18:02:36 +05:30
Each feature is protected using:
@FeatureSwitch ("FLAG_NAME")
2015-10-13 08:32:15 +02:00
2026-02-08 18:02:36 +05:30
---
2015-10-13 08:32:15 +02:00
2023-11-24 19:21:08 +01:00
2026-02-08 18:02:36 +05:30
## Feature Flag Capabilities
2015-10-13 08:32:15 +02:00
2026-02-08 18:02:36 +05:30
### Global Enable/Disable
Turns feature fully on/off.
2022-01-10 10:46:26 +00:00
2026-02-08 18:02:36 +05:30
### Whitelist
Specific users always enabled.
2015-10-13 08:32:15 +02:00
2019-06-21 14:45:39 +01:00
2026-02-08 18:02:36 +05:30
### Blacklist
Specific users always blocked.
2023-04-14 12:09:34 +01:00
2026-02-08 18:02:36 +05:30
### Percentage Rollout
Deterministic hash-based rollout.
---
## API — Feature Flag Management
Base Path:
`/api/flags`
### Create Flag
POST `/api/flags`
`{ "flagName": "ADD_PET", "flagEnabled": true, "rolloutPercentage": 100 }`
Returns **201 CREATED**
---
### List Flags
2023-04-14 12:09:34 +01:00
2026-02-08 18:02:36 +05:30
GET `/api/flags`
2023-04-14 12:09:34 +01:00
2026-02-08 18:02:36 +05:30
---
2021-11-24 07:45:21 +00:00
2026-02-08 18:02:36 +05:30
### List Flag based on flag name
2021-11-24 07:45:21 +00:00
2026-02-08 18:02:36 +05:30
GET `/api/flags/{name}`
2013-02-21 09:44:38 +08:00
2023-11-24 19:21:08 +01:00
2026-02-08 18:02:36 +05:30
GET `/api/flags`
2023-11-24 19:21:08 +01:00
2013-02-21 09:44:38 +08:00
2026-02-08 18:02:36 +05:30
---
2013-02-21 09:44:38 +08:00
2026-02-08 18:02:36 +05:30
### Delete Flag
2023-11-24 19:21:08 +01:00
2013-02-21 10:10:41 +08:00
2026-02-08 18:02:36 +05:30
DELETE `/api/flags/{name}`
2023-11-24 19:21:08 +01:00
2026-02-08 18:02:36 +05:30
Returns **204 NO CONTENT**
2023-11-24 19:21:08 +01:00
2018-10-08 10:10:41 +02:00
2026-02-08 18:02:36 +05:30
---
2018-10-08 10:10:41 +02:00
2018-10-09 08:44:25 +02:00
2026-02-08 18:02:36 +05:30
## Design Decisions
2018-10-08 10:10:41 +02:00
2026-02-08 18:02:36 +05:30
- Built custom annotation + AOP instead of libraries (per requirement)
- DB-backed persistence
- Deterministic rollout using hash bucket
- Default behavior = disabled if flag missing
- No authentication on flag APIs (as per requirement)
2018-10-05 15:38:11 +01:00
2013-02-21 10:10:41 +08:00
2026-02-08 18:02:36 +05:30
---
2013-05-06 10:57:17 +08:00
2026-02-08 18:02:36 +05:30
## Edge Cases Handled
2016-11-12 12:13:44 +01:00
2026-02-08 18:02:36 +05:30
- Missing flag is treated as feature disabled
- 0% rollout is treated as always blocked
- 100% rollout is always enabled
- Blacklist overrides whitelist
2013-05-06 10:57:17 +08:00
2026-02-08 18:02:36 +05:30
---
2013-05-06 10:57:17 +08:00
2026-02-08 18:02:36 +05:30
## Package Structure (New Code)
2013-05-06 10:57:17 +08:00
2026-02-08 18:02:36 +05:30
The new code is placed in a separate package to maintain modularity and separation of concerns:
- `org.springframework.examples.petclinic.featureflags` — Core feature flag system (annotation, aspect, service, repository)
2013-05-06 10:57:17 +08:00
2026-02-08 18:02:36 +05:30
## Credits
2015-10-28 18:39:02 +01:00
2026-02-08 18:02:36 +05:30
Base project forked from:
2015-10-28 18:39:02 +01:00
2026-02-08 18:02:36 +05:30
Spring PetClinic — https://github.com/spring-projects/spring-petclinic
2015-10-28 18:39:02 +01:00
2026-02-08 18:02:36 +05:30
Feature Flag system implementation added as part of assignment work.