0
0
Spring Bootframework~5 mins

Feature flags concept in Spring Boot

Choose your learning style9 modes available
Introduction

Feature flags let you turn parts of your app on or off without changing the code. This helps you test new features safely and control what users see.

You want to try a new feature with a small group of users before everyone.
You need to quickly disable a feature if it causes problems.
You want to release features gradually over time.
You want to run A/B tests to see which feature version works better.
You want to separate deployment from feature release.
Syntax
Spring Boot
public class FeatureFlagService {
    private Map<String, Boolean> featureFlags = Map.of(
        "newDashboard", true,
        "betaFeature", false
    );

    public boolean isFeatureEnabled(String featureName) {
        return featureFlags.getOrDefault(featureName, false);
    }
}

This example uses a simple map to store feature flags.

In real apps, flags can come from config files, databases, or remote services.

Examples
Check if the "newDashboard" feature is on, then show the right UI.
Spring Boot
if (featureFlagService.isFeatureEnabled("newDashboard")) {
    // show new dashboard
} else {
    // show old dashboard
}
Return different pages based on the feature flag.
Spring Boot
@GetMapping("/home")
public String home() {
    if (featureFlagService.isFeatureEnabled("betaFeature")) {
        return "betaHome";
    }
    return "regularHome";
}
Sample Program

This Spring Boot example shows a service holding feature flags and a controller that returns if a feature is enabled or not.

Spring Boot
package com.example.featureflags;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Service
public class FeatureFlagService {
    private final Map<String, Boolean> featureFlags = Map.of(
        "newFeature", true,
        "experimentalUI", false
    );

    public boolean isFeatureEnabled(String featureName) {
        return featureFlags.getOrDefault(featureName, false);
    }
}

@RestController
public class FeatureController {

    private final FeatureFlagService featureFlagService;

    public FeatureController(FeatureFlagService featureFlagService) {
        this.featureFlagService = featureFlagService;
    }

    @GetMapping("/feature-status")
    public String featureStatus() {
        if (featureFlagService.isFeatureEnabled("newFeature")) {
            return "New Feature is ENABLED";
        } else {
            return "New Feature is DISABLED";
        }
    }
}
OutputSuccess
Important Notes

Feature flags help avoid risky code changes by controlling features at runtime.

Keep your flags organized and remove old ones to avoid clutter.

Use descriptive names for flags so everyone understands their purpose.

Summary

Feature flags let you turn features on or off without code changes.

They help test, release, and control features safely.

In Spring Boot, you can implement flags with a service and check them in your controllers.