Skip to content

True immutability with @ConfigurationProperties and @ConstructorBinding  #2770

Open

Description

@kraspasov

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

Activity

  1. 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)[/+] on Feb 27, 2025
  2. 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 [/+] on Feb 28, 2025
  3. ryanjbaxter commented on Feb 28, 2025

    @ryanjbaxter
    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

  4. ruthst00 commented on Nov 9, 2026

    @ruthst00

    @ryanjbaxter, on branch **5.0.x (5.0.7-SNAPSHOT), the requested behavior already works with correct bean registration — no production-code fix is necessary. See PR #3391 for details.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址