I am trying to find a way how to programatically create bean in quarkus DI, but without success. Is it possible in this framework? It seems that BeanManager
does not implement the needed method yet.
Quarkus ArC is a build-time oriented dependency injection based on CDI 2.0. In this blogpost, we're going to explain the relationship to the specification and describe some of the benefits and drawbacks of the build-time processing design.
Quarkus makes registration of reflection in an extension a breeze by using ReflectiveClassBuildItem , thus eliminating the need for a JSON configuration file.
Config is not proxyable because it has no no-args constructor - Managed Bean [class xx. Config] with qualifiers [@Default @Named @Any]. The possible reason for this is that a non-dependent scoped bean is required to provide a public no-args constructor for CDI, so that it can pick up the desired proxy bean at runtime.
First, we should clarify what "programatically create bean" exactly means.
But first of all, we should define what "bean" means. In CDI, we talk about beans in two meanings:
javax.enterprise.inject.spi.Bean
The metadata is usually derived from the application classes. Such metadata are "backed by a class". By "backed by a class" I mean all the kinds described in the spec. That is class beans, producer methods and producer fields.
Now, if you want to programatically obtain a component instance (option 2), you can:
javax.enterprise.inject.Instance
; see for example the Weld docs
CDI.current().select(Foo.class).get()
Arc.container().instance(Foo.class).get()
However, if you want to add/register a component metadata that is not backed by a class (option 2), you need to add an extension that makes use of quarkus-specific SPIs, such as BeanRegistrar.
If you are looking for Quarkus equivalent of Spring @Configuration
then you want "bean producer" (as mentioned in comments above)
Here is an example(koltin) of how to manually register a clock:
import java.time.Clock
import javax.enterprise.context.ApplicationScoped
import javax.enterprise.inject.Produces
@ApplicationScoped
class AppConfig {
@Produces
@ApplicationScoped
fun utcClock(): Clock {
return Clock.systemUTC()
}
}
@Produces
is actually not required if method is already annotated with @ApplicationScoped
@ApplicationScoped
at class level of AppConfig
is also not requiredAlthough, I find those extra annotations useful, especially if are used to Spring.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With