You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: I have posted this in microprofile-config as well; I'm not sure if this is a microprofile-config thing or due to the smallrye implementation eclipse/microprofile-config#752
Using microprofile-config 3 and a class annotated with @ConfigProperties does not function as a managed bean that can @Produces other beans.
No-args constructor class that is annotated with @ConfigProperties
@ConfigProperties(prefix="server")
@Dependent
class MyClass {
private int port;
private String host;
...
// this doesn't work
// Weld SE logs do not show this as producer
@Produces
SomeBeanClass producesSomeBeanClass() {
// Create a bean here for @Inject elsewhere
}
}
The MyClass instance is create as expected with the fields injected by microprofile-config. But the @Produces method is not recognised as a bean producer.
Since MyClass has a no-args constructor, I was expecting Weld SE to recognise
it as a Managed Bean and use it as a producer.
Does having a @ConfigProperties annotation "unrecognise" the class as a managed bean?
We veto classes annotated with @ConfigProperties, to provide our configurator bean and create the instance as expected (with the configuration values).
If you let @ConfigProperties be a managed bean, you just get an empty shell. I don't think there is a way to override a managed bean's constructor (or the way to retrieve instances). We can change the behavior if you know a better way to do it.
You could overwrite InjectionTarget#produce (and maybe InjectionTarget#inject) instead for @ConfigProperties, could you not? I wanted to try that for a while, but haven't found the time yet.
A similar thing that I've stumbled upon: It is currently not possible to use a record class to aggregate config properties, because the configurator bean is used instead of the canonical record constructor. That is, as I've noticed afterwards, in accordance with the spec which requires a no-arg constructor. Nevertheless, it seems like a desirable feature to have.
I have a working proof of concept now: https://github.com/JHahnHRO/ConfigProperties
It's an extension that realizes @ConfigProperties beans as ordinary managed beans (with a non-ordinary InjectionTarget). Therefore, this beans can still have producer methods.
I have included a small unit-test that demonstrates @ConfigProperties bean without no-args constructor and with producer methods.
Note: I have posted this in microprofile-config as well; I'm not sure if this is a microprofile-config thing or due to the smallrye implementation eclipse/microprofile-config#752
Using microprofile-config 3 and a class annotated with
@ConfigProperties
does not function as a managed bean that can@Produces
other beans.No-args constructor class that is annotated with
@ConfigProperties
The
MyClass
instance is create as expected with the fields injected by microprofile-config. But the@Produces
method is not recognised as a bean producer.Since
MyClass
has a no-args constructor, I was expecting Weld SE to recogniseit as a Managed Bean and use it as a producer.
Does having a
@ConfigProperties
annotation "unrecognise" the class as a managed bean?Reading about "Producer" methods: https://docs.jboss.org/weld/reference/latest/en-US/html/part1.html#_producer_methods ;
But
MyClass
does indeed seem to qualify as a managed bean: https://docs.jboss.org/weld/reference/latest/en-US/html/part1.html#_managed_beansThe text was updated successfully, but these errors were encountered: