Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable configuration of Spring boot

I'm trying to find a way to use the Immutables library to create data classes for holding the configuration of my Spring Boot application.

My data configuration class is:

@Value.Immutable
@JsonDeserialize(as = ImmutableAuthConfig.class)
public interface AuthConfig {
    String domain();
    String clientId();

    @Value.Redacted
    String clientSecret();
}

While the main configuration class is set up as

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class Config {
    private ImmutableAuthConfig auth;

    public AuthConfig getAuth() {
        return auth;
    }

    public void setAuth(ImmutableAuthConfig auth) {
        this.auth = auth;
    }
}

I've tried some variations of using either ImmutableAuthConfig or just AuthConfig as a field, but nothing improved the situation. The configuration did not get picked up, and the auth field of the configuration remains null after application start-up.

Replacing the contents of the AuthConfig class with a traditional POJO solves the issue, but I would prefer an immutable object. Is there any way to convince Spring to interface with the classes generated by the Immutables library?

like image 510
Henrik Aasted Sørensen Avatar asked Jun 28 '18 06:06

Henrik Aasted Sørensen


People also ask

What is @configuration spring boot?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.

What are the ways in which spring boot can read configuration?

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration.

What is @ConstructorBinding?

@ConstructorBinding. . This annotation tells Spring to bind our configuration properties using the provided constructor instead of using the setters.

What does @configuration do in spring?

@Configuration annotation indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.


1 Answers

The library's support for modifiable classes supplies an approach that is pretty close to what I was searching for.

@Value.Modifiable
public interface AuthConfig {
    String domain();
    String clientId();

    @Value.Redacted
    String clientSecret();
}

This creates the class ModifiableAuthConfig which provides an interface satisfying Spring's JavaBeanBinder, which is used for deserializing the configuration.

It's furthermore necessary to supply an instance of the mutable AuthConfig class, which Spring can then populate:

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class Config {
    private ImmutableAuthConfig auth = ModifiableAuthConfig.create();

    public AuthConfig getAuth() {
        return auth;
    }
}

Any use of the loaded configuration can subsequently happen through the AuthConfig interface, which doesn't provide mutating methods.

like image 124
Henrik Aasted Sørensen Avatar answered Sep 18 '22 12:09

Henrik Aasted Sørensen