support conditional bean
See original GitHub issueDescription
I want to enable or disable Kafka consumer based on a runtime property, currently @IfBuildProperty doesn’t support runtime property.
https://docs.micronaut.io/latest/guide/#conditionalBeans
This @Requires annotation looks good:
@ApplicationScoped
@Requires(property="my.consumer.enabled", value="true")
public class MyConsumer {
@Incoming("topic")
public void consume(Xxx x) {
}
}
Implementation ideas
Implementation something like Micronaut @Requires.
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (11 by maintainers)
Top Results From Across the Web
Conditional Beans with Spring Boot - Reflectoring
Declaring Conditional Beans ... Anywhere we define a Spring bean, we can optionally add a condition. Only if this condition is satisfied will...
Read more >Spring Conditional Annotations - Baeldung
We can apply the above example to any bean declared with the @Component, @Service, @Repository, or @Controller annotations.
Read more >Can we declare spring bean conditionally? - Stack Overflow
@ConditionalOnProperty(value="usenew", on=true, propertiesBeanName="myprops") @Service("service") public class newService implements ServiceFunction{ // some ...
Read more >Spring Boot Conditional beans - ZetCode
Spring Boot Conditional beans tutorial shows how to register beans into the Spring Boot application context based on conditions.
Read more >Spring - @Bean conditional registration - LogicBig
Spring 4, introduced a new annotation @Conditional, which is meant to be used on @Bean methods or (in case of @ComponentScan) with ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Very valid point👍🏻 I can’t figure out a build time solution now except we explicitly forbidden this usage😄
Understand, detail is the devil 😃
That won’t work. Imagine you have a bean
Foo extends Bar
with@Requires(property="my.consumer.enabled", value="true")
and a beanBaz extends Bar
with@Requires(property="my.consumer.enabled", value="false")
. If you ignore@Requires
at build time and perform the full validation then@Inject Bar
would fail with ambiguous dependency error - there are multiple beans that satisfy this injection point.The CDI container is used for both framework beans and application beans. The condition
if (required(objA_id))
is an oversimplification that does not reflect the real behavior.