Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate a configuration property only if a certain profile is active?

Is there a way to expect a property not to be null, only if a certain profile is active?

@Data
@Component
@ConfigurationProperties(prefix = "sample")
@Validated
public class SampleProperties {

    @NotNull
    private String sample1;

    @NotNull
    @Valid
    private MoreProperties more = new MoreProperties()


    @Data
    public static class MoreProperties {

        @NotNull // ← This should not be null only if the prod profile is active
        private String sample2;

    }
}
like image 574
Behrang Avatar asked Sep 14 '25 08:09

Behrang


1 Answers

How about using two @ConfigurationProperties classes.

One like this:

@Validated
@ConfigurationProperties(prefix = "sample")
public class SampleProperties {

  @NotNull
  private String sample1;

  // Getters + Setters
}

And one like this

@Validated
@ConfigurationProperties(prefix = "sample")
public class SamplePropertiesForProfile extends SampleProperties {

  @NotNull // ← This should not be null only if the prod profile is active
  private String sample2;

  // Getters + Setters
}

And using these classes as beans only if the correct profile is active like this. The @EnableConfigurationProperties provides the @ConfigurationProperties class as bean. I prefer this over the @Component annotation on the @ConfigurationProperties class, because it's guaranteed that the bean is only created if it is required. I see @ConfigurationProperties as a 'dumb' container of properties that does not know anything about their usage and therefore it can't know when its required and when it is not.

@Configuration
@Profile("!yourDesiredProfile")
@EnableConfigurationProperties(SampleProperties.class)
public class SampleNotProfileConfiguration {

  private readonly SampleProperties sampleProperties;

  @Autowired
  public SampleNotProfileConfiguration(SampleProperties sampleProperties){
    this.sampleProperties = sampleProperties;
  }

  // Configure your beans with the properties required for this profile
}


@Configuration
@Profile("yourDesiredProfile")
@EnableConfigurationProperties(SamplePropertiesForProfile .class)
public class SampleProfileConfiguration {

  private readonly SamplePropertiesForProfile samplePropertiesForProfile ;

  @Autowired
  public SampleProfileConfiguration (SamplePropertiesForProfile samplePropertiesForProfile ){
    this.samplePropertiesForProfile = samplePropertiesForProfile ;
  }

  // Configure your beans with the properties required for this profile
}

Doing this you have an explicit configuration for each profile with explicit configuration properties. Even the name @ConfigurationProperties already states that this class contains properties of a configuration. These properties should match the requirements of the @Configuration class. Therefore if a bean should be configured in a different way for a given profile it should be another @Configuration which then requires another explicit @ConfigurationProperties.

Can't verify this solution at the moment but for my understanding of @ConfigurationProperties this should work and looks more clean to me than mixing properties of different profiles.

like image 114
Yannic Klem Avatar answered Sep 15 '25 22:09

Yannic Klem