javaspring-bootspring-4

Spring @Conditional based on a value in database table


Condition evaluation depends on a value provided in data base table

@Component
public class XYZCondition implements Condition{

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
              //based on value defined in DB should return true/false
    }

}

As Condition is executing very early, unable to fetch db value is there any alternate way to achieve this ?


Solution

  • Database values can be changed during application work, while it doesn't seem a good idea to reload application context. So I would recommended using configuration properties to choose which beans should be available in context.

    Moreover, there's a Spring Cloud Config that allows you to store configuration in git or some other storages. Its consumers may restart context once configuration changes. It seems worth talking a look at it as well.