question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Declared defaults do not get property references replace

See original GitHub issue

Assume you declare a configuration properties class like this:

@ConstructorBinding
@ConfigurationProperties("sample")
class SampleProperties {

  SampleProperties(@DefaultValue("${user.home}/foo") String location) { … }
}

If the application now configures the property sample.location to ${user.home}/bar, the constructor is called with /Users/…/bar, i.e. the dynamic reference to the user home folder has been replaced with the actual value. If the application does not configure an explicit value, the value handed into constructor is literally ${user.home}/foo, i.e. the reference to the user’s home directory is not resolved.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Feb 3, 2022

We talked about this and concluded that we’d like to align the behaviour of @DefaultValue with what would happen if you had written the same value in application .properties or application.YAML. You get conversion at the moment (so, for example, "3w" for a Duration would become a Duration of 3 weeks), but you don’t get placeholder replacement. For consistency, we’d like to get both.

We have some concerns about backward-compatibility so it’ll have to wait for 3.x. We’ve reopened #23164 to improve the docs in the meantime.

1reaction
wilkinsonacommented, Jan 20, 2022

As things stand, the desired behaviour can be achieved like this with setter-based binding:

@ConfigurationProperties("sample")
public class SampleMutableProperties {
    
    /**
     * Location to use. Defaults to a directory named example in the user's home directory.
     */
    private String location = System.getProperty("user.home") + "/example";

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

}

The same can be achieved with immutable configuration properties like this:

@ConstructorBinding
@ConfigurationProperties("sample")
public class SampleImmutableProperties {
    
    /**
     * Location to use. Defaults to a directory named example in the user's home directory.
     */
    private final String location;
    
    public SampleImmutableProperties(String location) {
        this.location = (location != null) ? location : System.getProperty("user.home") + "/example";
    }

    public String getLocation() {
        return location;
    }

}

The javadoc for @DefaultValue states that it “can be used to specify the default value when binding to an immutable property”. That’s not really accurate when you’re trying to turn SampleMutableProperties into something immutable. You have to perform the defaulting manually in the constructor as shown above instead.

Knowing that you’ve only got a string to work with, trying to use @DefaultValue("${user.home}/example") (particularly if you’re already familiar with Spring) seems quite logical to me but it won’t work as hoped as ${user.home} will be left as-is. If it worked as @odrotbohm expected and performed property placeholder resolution it would be more concise and also have the benefit over the field initializer of being able to document a sensible default value for the property automatically.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to reference another property in java.util.Properties?
Our implementation of this function looks up match group 1 in the properties or defaults to the match (i.e. does not actually do...
Read more >
How to: Declare and Call a Default Property - Visual Basic
Declare the property in the normal way. Do not specify the Shared or Private keyword. · Include the Default keyword in the property...
Read more >
Defaults and Assignments - HUD
This chapter is not applicable to FHA-insured healthcare loans. ... and interest on the loan is declared immediately due and payable, the date...
Read more >
Cascade, specificity, and inheritance - Learn web development
The rule that's styling your element may not be the one you expect, ... Note: On MDN CSS property reference pages, you can...
Read more >
Spring Boot Reference Documentation
If you do not already have Gradle installed, you can follow the instructions at gradle.org. Spring Boot dependencies can be declared by using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found