question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

support conditional bean

See original GitHub issue

Description

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:closed
  • Created 2 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
Diekencommented, Aug 10, 2021

Sorry I don’t get your point. When I say “@Requires annotation is ignored at build time”, I mean all beans are required at build time, just like there is no @Requires annotation at all, so Quarkus can do fully validation at build time.

That won’t work. Imagine you have a bean Foo extends Bar with @Requires(property="my.consumer.enabled", value="true") and a bean Baz 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.

Very valid point👍🏻 I can’t figure out a build time solution now except we explicitly forbidden this usage😄

According to native-image-pre-boot, it is an optimization for Quarkus framework, it won’t be affected if @requires isn’t used by Quarkus framework.

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.

Understand, detail is the devil 😃

1reaction
mkoubacommented, Aug 10, 2021

Sorry I don’t get your point. When I say “@Requires annotation is ignored at build time”, I mean all beans are required at build time, just like there is no @Requires annotation at all, so Quarkus can do fully validation at build time.

That won’t work. Imagine you have a bean Foo extends Bar with @Requires(property="my.consumer.enabled", value="true") and a bean Baz 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.

According to native-image-pre-boot, it is an optimization for Quarkus framework, it won’t be affected if @requires isn’t used by Quarkus framework.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found