0

I want to use and Enumeration type to represent feature flags in my application. The enumeration type has a state and a description. I want to be able do something like the following in my code;

FeatureFlag.FANCYFEATURE.isActive()

The isActive() method would then call out to a service class connecting to a database to fetch the features state.

However in my spring application its not possible inject a bean into an Enum as the Enum type is static.

Can someone recommend a clean way to so this?

2 Answers 2

4

The following statement should tell you something is wrong

The isActive() method would then call out to a service class connecting to a database to fetch the features state

If you are fetching the state, we can assume it might be different at different times, ie. not a constant. Don't use enum for this.

And if you did, to answer

Can someone recommend a clean way to so this?

There is no way to inject beans into it, like you said.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly... sounds like a misuse of enum here.
3

By all means use an enum for a list of features but please don't use the enum for mutable state.

You would be better off using a EnumMap<Enum,State> structure to store mutable state against an enum.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.