DEV Community

Vikas Garg
Vikas Garg

Posted on

Feature Flags with Spring Boot

Feature Flags is a very broad topic and have been coming back up regularly irrespective of what language you use. There are multiple platforms available which provide extensive support for managing your features external to the code or deployed as part of the code. Some of the examples are,

  • LaunchDarkly
  • Togglz
  • Property (File) based and many more.

Table Of Contents

Problem at hand

This article specifically targets a case where you can add a toggle check before any method you want before actually getting into the method execution. Be it at a controller level to prevent an API to be executed OR a service method OR a repository method. This pattern can be extended with any feature toggle implementation you use.

Code for the blog can be found here.

GitHub logo vikasgarghb / blog-feature-flags-spring

Feature Flag Annotation usage with Spring Boot.

Project Setup

To begin with you will need,

  • Java 11+
  • Gradle 7+
  • IDE of your choice
  • Spring Boot 2.5.4+

Dependencies

Apart from the other spring dependencies, you will need to include spring-aop and aspectjweaver. There are much documentation available for Aspect based programming with Spring. Won't go into much details about it. But one thing which this article focuses on is how aspect can be used to call a method before executing actual method.

AspectJ's @Before annotation allows us to accomplish this. As long as correct pointcut expression is provided, the method decorated with this annotation will be called before the actual method.

For eg,

@Before("execution (* blog.vgarg.features..*(..)) && @annotation(checkFeatureFlag)")
public void checkFeatureFlag(JoinPoint joinPoint, CheckFeatureFlag checkFeatureFlag) {
    String flag = checkFeatureFlag.flag();
    if (!featureFlagService.isFeatureFlagSet(flag)) {
        throw new FeatureNotEnabledException();
    }
}
Enter fullscreen mode Exit fullscreen mode
@CheckFeatureFlag(flag = "flags.goodmorning")
@GetMapping("/good-morning")
public ResponseEntity<String> getGoodMorning() {
    return new ResponseEntity<>(greetingService.getGoodMorning(), HttpStatus.OK);
}
Enter fullscreen mode Exit fullscreen mode

checkFeatureFlag method will be executed by spring before getGoodMorning method gets executed.

Feature Loading

So, for this post, I am using file based features but the approach can be extended to use with any other feature provider.

public interface FeatureFlagService {
    boolean isFeatureFlagSet(String flag);
}
Enter fullscreen mode Exit fullscreen mode

I have defined an interface with a simple single method to check the status of any given feature flag. As mentioned for this post the implementation reads the toggle from a file named, featureflags.properties residing in src/main/resources (can be anywhere as long as it is on classpath).

Enhancement

As mentioned before this can be extended for other feature support providers. For eg, with launch darkly the implementation will involve using the SDK provided by them. As long as the LD setup is done correct way, using their SDK is pretty simple. More can be read here.

Remarks

This is very bare minimum implementation of the functionality and has the capability of being extended into more. This only applies when the feature check needs to be applied before the actual method. For eg, if a new API needs to be deployed but not released, this check can come in handy.

But as any other solution this is not one size fits all solution. Would be happy to hear from the community what solutions have they tried.

Discussion (1)

Collapse
alexeboswell profile image
Alex Boswell

Great lightweight implementation of feature flags. If you're looking to take feature flags further, I would check out open source project Flagsmith - github.com/Flagsmith/flagsmith