Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does @ConfigProperties on a class prevent Weld SE bean management? #825

Open
space88man opened this issue Sep 17, 2022 · 4 comments
Open
Labels
enhancement New feature or request

Comments

@space88man
Copy link

space88man commented Sep 17, 2022

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?

Reading about "Producer" methods: https://docs.jboss.org/weld/reference/latest/en-US/html/part1.html#_producer_methods ;

A producer method must be a non-abstract method of a managed bean class or session bean class.

But MyClass does indeed seem to qualify as a managed bean: https://docs.jboss.org/weld/reference/latest/en-US/html/part1.html#_managed_beans

@radcortez
Copy link
Member

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.

@JHahnHRO
Copy link

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.

@JHahnHRO
Copy link

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.

@radcortez
Copy link
Member

Feel free to submit a PR, and I'll have a look.

@radcortez radcortez added the enhancement New feature or request label Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants