Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

True immutability with @ConfigurationProperties and @ConstructorBinding #2770

Open
kraspasov opened this issue Feb 27, 2025 · 1 comment
Open

Comments

@kraspasov
Copy link

kraspasov commented Feb 27, 2025

The current behavior has been reported previously here #1547.

I want to lay down the arguments why this doesn't make sense to work this way.

The current situation is that the feature works this way and is expected to work like this to achieve immutable configurations upon context refresh, in other words, those are just beans ignored by the refresh event, even if you have them marked as @RefreshScope.

The first argument against "this is how it should work" is, if I want to not refresh some properties, i.e. refresh context events are not affecting the configuration bean, I will just not put the @RefreshScope annotation.

The second argument against this statement in the original issue - "protecting jr developers during testing is not a strong enough argument for what is likely a non trivial feature to support". In my team we have had an actual production issue by the same thing.

I.e. when I think of immutability, I think of it that a single instance of bean/object is immutable. If I want it to be @RefreshScope, then I want to be able to create new bean instances with a new immutable state. If I want a bean instantiation immutability, I will just not make it part of the refresh scope event.

In simple terms I want to have the following code:

@RefreshScope
@Configuration
@ConfigurationProperties(prefix = "config.app")
public class MyConfig {

  private Map<String, String> map;

@ConstructorBinding
public MyConfig(Map<String, String> map) {
   map = Collections.unmodifiableMap(map);
}

And I want the following behavior:

  1. The map inside the MyConfig bean is immutable runtime. I.e. stream, filters, etc. can't modify it.
  2. I want the map property of the MyConfig to be immutable, i.e. no setters. I don't want someone accidentally to change the map reference.
  3. I still want to be able to update the properties on refresh application context events.

This way I am ensuring no more bugs on production from filters, streams, etc. and changed object references. I.e. true Java immutability. The way the feature is working currently I can't ever achieve true immutability of the bean's properties runtime, while still be able to update/refresh the properties

@kraspasov kraspasov changed the title True immutability with @ConfigurationProperties and @ConstructorBinding (issue #1547) True immutability with @ConfigurationProperties and @ConstructorBinding (reference https://github.com/spring-cloud/spring-cloud-config/issues/1547) Feb 27, 2025
@ryanjbaxter ryanjbaxter changed the title True immutability with @ConfigurationProperties and @ConstructorBinding (reference https://github.com/spring-cloud/spring-cloud-config/issues/1547) True immutability with @ConfigurationProperties and @ConstructorBinding Feb 28, 2025
@ryanjbaxter
Copy link
Contributor

I am not sure this is even possible. It might be out of our control if the field is immutable. It would need to be investigated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants