Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Good practice to create beans from dependencies on condition

$
0
0

I have:

  1. module app-core
  2. module reporting-api
  3. multiple moduiles such as reporting-elastic, reporting-kafka and so on.

app-core has dependency to reporting-api and also to reporting implementations

reporting implementations have dependency to reporting-api

Now in the app-core there is property myApp.reporting = ELASTIC | NoOP | KAFKA , here user select what implementation of reporting service should be created.

In each of the report implementation modules, i create the bean, so for example for elastic i have config:

@Configuration@ConditionalOnProperty(value = "myApp.reporting", havingValue = "ELASTIC")public class Configuration {    @Bean    ReportService elasticReportSertice(){      return new ElasticReportService();    }}

in reporting-kafka i have:

@Configuration@ConditionalOnProperty(value = "myApp.reporting", havingValue = "KAFKA")public class Configuration {    @Bean    ReportService kafkReportService(){      return new KafkaReportService();    }}

And so on, now however when NoOP is specified ( or NO property is specified at all) i want to create NoOp version of the reporting service.

Currently, i do that in configuration in app-core

@Configuration@ConditionalOnProperty(value = "myApp.reporting", havingValue = "NoOp", matchIfMissing = true)public class AppCoreConfiguration {    @Bean    ReportService noOpService(){      return new NoOpReportService();    }}

This works, however having the bean definitions shattered across multiple config files seems pretty chaotic, is there some better pattern or more correct way how to achieve this?

Thanks for help!


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>