0

I have application-sample.YAML file where I have data. After loading the data, based on certain fields. I want to decide which few components to load or not to load. I can see this below condition class loads first, as a result, I am getting the data null because this condition class is loading first before my Data loaded.

@Configuration
public class AwsCondition implements Condition {
    
    @Autowired
    MyTestData data;
    
    @Override       
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // TODO Auto-generated method stub
        log.info("inside...........condition");
        if(data.getListeners().size()>1){
            return true;
        }
        return false;
    }

}

MyTestData.java

@Slf4j
@Data
@ConfigurationProperties
@Component
public class MyTestData implements InitializingBean {

List<Listener> listeners = new ArrayList<>();

    @Data
    public static class Listener {
        private String type;
        private String name;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        
        if (this.getListeners().isEmpty()) {
            log.info("Nothing  configured. Please verify application-test.yml is configured properly.");
            System.exit(1);
        }
    }
}

ConditionTest class

@Component
@Conditional(AwsCondition.class)
public class TestS3ListenerRouter extends RouteBuilder {
 //My all logic lying here
}
João Dias
  • 16,277
  • 6
  • 33
  • 45
JDGuide
  • 6,239
  • 12
  • 46
  • 64

1 Answers1

0

Try the following:

@Component
public class AwsCondition implements Condition {
    
    @Autowired
    MyTestData data;
    
    @Override       
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // TODO Auto-generated method stub
        log.info("inside...........condition");
        if(data.getListeners().size()>1){
            return true;
        }
        return false;
    }
}
João Dias
  • 16,277
  • 6
  • 33
  • 45