spring-cloud / spring-cloud/spring-cloud-config

True immutability with @ConfigurationProperties and @ConstructorBinding

Open
#2,770 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted
Dominant language
Java
Stars
2k
Forks
1.3k
Avg merge
2d 59m
Merged PRs (30d)
16

Description

The current behavior has been reported previously here https://github.com/spring-cloud/spring-cloud-config/issues/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked prior issue and the current handling of @ConfigurationProperties, @ConstructorBinding, and @RefreshScope. Define how refreshed instances should preserve constructor-bound, immutable properties while still updating on context refresh. Done means the requested map and property immutability coexist with refresh updates, with behavior verified by appropriate tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.