📖 Scenario: You are building a Spring Boot web application that needs to enable or disable certain features without changing the code. This is useful when you want to test new features with some users or turn off features quickly if there are issues.
🎯 Goal: Create a simple Spring Boot application that uses a feature flag to control whether a greeting message is shown or not.
📋 What You'll Learn
Create a configuration class with a feature flag variable
Use the feature flag in a controller to decide if the greeting message is returned
Create a REST endpoint /greet that returns the greeting only if the feature flag is enabled
Use Spring Boot annotations and patterns correctly
💡 Why This Matters
🌍 Real World
Feature flags let developers turn features on or off without changing code or redeploying. This helps test new features safely and roll back quickly if needed.
💼 Career
Understanding feature flags is important for software developers working on large applications that require flexible feature management and safer deployments.
Progress0 / 4 steps
1
Create the Feature Flag Configuration
Create a Spring Boot configuration class called FeatureConfig with a boolean variable isGreetingEnabled set to true.
Spring Boot
Hint
Use @Configuration annotation and declare isGreetingEnabled as a public boolean variable set to true.
2
Inject the Feature Flag into Controller
Create a Spring Boot REST controller class called GreetingController. Inject the FeatureConfig class using constructor injection and store it in a private final variable called featureConfig.
Spring Boot
Hint
Use constructor injection to assign the featureConfig variable.
3
Add the Greeting Endpoint with Feature Flag Logic
In GreetingController, add a method greet() mapped to GET /greet. Return the string "Hello, User!" only if featureConfig.isGreetingEnabled is true. Otherwise, return an empty string.
Spring Boot
Hint
Use @GetMapping("/greet") and check the feature flag inside the method to decide the return value.
4
Complete the Spring Boot Application Class
Create the main Spring Boot application class called FeatureFlagApplication with the @SpringBootApplication annotation and a main method that runs the application.
Spring Boot
Hint
Use @SpringBootApplication and a main method to start the Spring Boot app.
Practice
(1/5)
1. What is the main purpose of using feature flags in a Spring Boot application?
easy
A. To improve database performance
B. To manage application logging levels
C. To handle user authentication
D. To enable or disable features without changing the code
Solution
Step 1: Understand feature flags concept
Feature flags allow toggling features on or off dynamically without redeploying code.
Step 2: Identify the main use in Spring Boot
In Spring Boot, feature flags help control feature availability safely during runtime.
Final Answer:
To enable or disable features without changing the code -> Option D
Quick Check:
Feature flags = toggle features dynamically [OK]
Hint: Feature flags toggle features without code changes [OK]
Common Mistakes:
Confusing feature flags with database optimization
Thinking feature flags manage authentication
Assuming feature flags control logging
2. Which of the following is the correct way to check a feature flag in a Spring Boot controller method?
easy
A. if(featureFlagService.isFeatureEnabled("newFeature")) { ... }
B. if(featureFlagService.checkFlag("newFeature")) { ... }
C. if(featureFlagService.enableFeature("newFeature")) { ... }
D. if(featureFlagService.toggleFeature("newFeature")) { ... }
Solution
Step 1: Recall typical method naming for feature flags
Common method to check if a feature is enabled is named like isFeatureEnabled.
Step 2: Match method name with options
Only if(featureFlagService.isFeatureEnabled("newFeature")) { ... } uses isFeatureEnabled which clearly indicates checking status.
Final Answer:
if(featureFlagService.isFeatureEnabled("newFeature")) { ... } -> Option A
Quick Check:
Check feature flag with isFeatureEnabled() [OK]
Hint: Look for method named isFeatureEnabled() to check flags [OK]
Common Mistakes:
Using methods that sound like they change state (enableFeature, toggleFeature)
Using incorrect method names not standard in feature flag services
3. Given the following Spring Boot code snippet, what will be the output if the feature flag "betaFeature" is disabled?
if(featureFlagService.isFeatureEnabled("betaFeature")) {
System.out.println("Beta feature is ON");
} else {
System.out.println("Beta feature is OFF");
}
medium
A. Beta feature is ON
B. Beta feature is OFF
C. No output
D. Runtime error
Solution
Step 1: Understand the condition check
The code prints "Beta feature is ON" only if the flag is enabled (true).
Step 2: Analyze the flag state given
The flag "betaFeature" is disabled, so isFeatureEnabled returns false, triggering the else block.
Final Answer:
Beta feature is OFF -> Option B
Quick Check:
Flag disabled = else block runs [OK]
Hint: Disabled flag triggers else block output [OK]
Common Mistakes:
Assuming disabled flag prints ON message
Expecting no output when else block exists
Thinking code throws error on false flag
4. Identify the error in this Spring Boot feature flag usage:
C. Missing parentheses when calling isFeatureEnabled method
D. Feature flag service not injected
Solution
Step 1: Check method call syntax
isFeatureEnabled is a method and must be called with parentheses ().
Step 2: Verify other code parts
HTTP annotation and return type are correct; no injection info given but not error here.
Final Answer:
Missing parentheses when calling isFeatureEnabled method -> Option C
Quick Check:
Method calls need () [OK]
Hint: Method calls always need parentheses () [OK]
Common Mistakes:
Forgetting () after method names
Confusing HTTP method annotations
Assuming return type must be void
5. You want to implement a feature flag in Spring Boot that disables a new payment method only for users in a "betaTesters" group. Which approach correctly combines feature flags with user context?
hard
A. Check if feature flag is enabled and user belongs to betaTesters before allowing payment
B. Enable feature flag globally and ignore user groups
C. Disable feature flag and allow all users access
D. Use feature flag only in frontend without backend checks
Solution
Step 1: Understand combining feature flags with user context
Feature flags can be combined with user group checks to control feature access precisely.
Step 2: Evaluate options for correct logic
Check if feature flag is enabled and user belongs to betaTesters before allowing payment correctly checks both the flag and user group before enabling the feature.
Final Answer:
Check if feature flag is enabled and user belongs to betaTesters before allowing payment -> Option A
Quick Check:
Feature flag + user group check = controlled access [OK]
Hint: Combine flag check with user group for precise control [OK]
Common Mistakes:
Ignoring user groups when using feature flags
Relying only on frontend checks
Enabling feature globally without restrictions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.