open-feature/java-sdk

Question: Client as spring boot managed bean

Closed this issue · 2 comments

Hi,

I have a question regarding the tutorial https://openfeature.dev/docs/tutorials/getting-started/java

In this tutorial, the spring configuration is as follows

`@Configuration
public class OpenFeatureBeans {

@Bean
public OpenFeatureAPI OpenFeatureAPI() {
    final OpenFeatureAPI openFeatureAPI = OpenFeatureAPI.getInstance();

    return openFeatureAPI;
}

}`

Is there any reason why I shouldn't instantiate the client as a bean instead so that I can re-use the client rather than asking the OpenFeatureAPI for a client instance in each location where am using it?

Hey @GianniGiglio . Either should work, but one advantage of getting a client each time is that you can set a context on a client instance... you could, for instance, put request-specific data on the client and use that for the duration of the request, if you configure your spring setup that way:

client.setEvaluationContext(new ImmutableContext(...{some request-specific values, userId, email, etc}...));

Clients are relatively lightweight, so creating a lot of them (1 per request, for instance) shouldn't be a problem. The only related resource issue to be aware of is that you shouldn't add event handlers to clients that you generate on the fly like this, unless you are very careful to remove them.

Thank you the answer is sufficient to close the ticket