Injecting a named String using CDI

后端 未结 1 736
后悔当初
后悔当初 2021-01-12 11:45

I want to have a configuration parameter injected this way:

public class MyManagedBean {
    @Inject
    public MyManagedBean(@Named(\"user\") String user){
         


        
相关标签:
1条回答
  • 2021-01-12 12:16

    It looks like you need a default (no-arg) constructor for your MyManagedBean to make it proxyable. I am not really sure about why it is needed, since MyManagedBean is a @Dependent bean and so is not proxied AFAIK; I do not know even why a proxyable bean needs a default constructor, to be honest This seems to be an implementation detail or a little point from CDI specification that was ignored. Anyway, I bet it can make a good new question :)

    EDIT: I discovered why a proxyable bean needs a default construction. It is no mistery, actually: since the proxy of a bean is a subclass of the same bean, the proxy needs to call a super() constructor in its own construction. If it has no non-private default constructor, it does not know which constructor to call. One can even imagine a scenario where the injected constructor is called to create the proxy, but I do not know which kind of complexity it can add to a CDI implementation; it may not be so easy to do...

    0 讨论(0)
提交回复
热议问题