SoFunction
Updated on 2025-03-01

Solve the problem of @ConfigurationProperties invalid in java

The value of the configuration file cannot be obtained when reading the configuration file (or) using @ConfigurationProperties.

Original code:

@Component
@ConfigurationProperties(prefix = "aichatting")
public class AIChattingConfig
{
    //Resource mapping path    private static String profile;

    //Resource mapping path prefix    public static final String RESOURCE_PREFIX = "/profile";

    public static String getProfile()
    {
        return profile;
    }

    public void static setProfile(String profile)
    {
         = profile;
    }
}

Configuration file():

aichatting:
  profile: D:/AI-Chatting/uploadPath

For convenience, I set the profile property in the configuration class to static. If the read is successful, execute() will return as the value in the configuration file (D:/AI-Chatting/uploadPath)

reason:@ConfigurationProperties cannot recognize static methods, so the setter of the property needs to be non-static

Modify the code:

@Component
@ConfigurationProperties(prefix = "aichatting")
public class AIChattingConfig
{
    //Resource mapping path    private static String profile;

    //Resource mapping path prefix    public static final String RESOURCE_PREFIX = "/profile";

    public static String getProfile()
    {
        return profile;
    }

    //@ConfigurationProperties cannot recognize static methods, so the setter needs to be non-static    public void setProfile(String profile)
    {
         = profile;
    }
}

This is the article about solving the problem of @ConfigurationProperties invalidation in java. For more related java @ConfigurationProperties invalidation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!