You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@openwebbeans.apache.org by Martin Strohal <ma...@1und1.de> on 2014/09/12 15:56:53 UTC

Only no-args constructor executed

Hi,

I have a question about ApplicationScoped CDI beans with constructor
injection.
My bean has two constructors: one default without parameters and one
with @Inject.
If I inject this bean in another one, only the default constructor is
executed (I've put some logging messages in both constructors). The
injection constructor is not executed, so the field is null.
How can that be? What am I doing wrong?

(BTW: I'm using openwebbeans 1.1.8 in TomEE 1.5.2)

Thanks,
Martin

Re: Only no-args constructor executed

Posted by Martin Strohal <ma...@1und1.de>.
Hi,

thank you for your very hepful answers!
I have found the problem. My ApplicationScoped classes are in a library
and have abstract base classes (because there are implementations for
CDI and Spring). The abstract classes have some final methods. And
within the final methods it seems not to be possible to access injected
references.
The methods are not final anymore now - and it works :)

Thanks - und ebenfalls viele Gruesse

Martin


Am 12.09.2014 17:23, schrieb Mark Struberg:
> It's exactly as Joe already explained. Whenever we use client proxies
> in JavaEE (which is for *all* NormalScoped CDI beans plus all
> NoInterfaceView EJBs) we generate a dynamic subclass on the fly
> ('Contextual Reference') which overloads all the methods and delegates
> through to the 'Contextual Instance'
>
> For a native class
>
> public class User {
>   private String name;
>   public String getName() { return name; }
>   public void setName(String name) {this.name=name;}
> }
>
> we will create a new class on the fly (see NormalScopedProxyFactory in
> our code) which is like:
>
> public class User$$OwbNormalScopeProxy extends User {
>   private User owbContextualInstanceProvider;
>
>   @Override
>   public String getName() { return
> owbContextualInstanceProvider.getName();};
>
>   @Override
>   public void setName(String name) {
> owbContextualInstanceProvider.setName(name); };
> }
>
> And of course this 'Contextual Reference' can only use the default
> constructor of the proxied class.
> Btw, this is one of the reasons why you should always use
> @PostConstruct and not the ct to initialize your beans.
>
> LieGrue,
> strub
>
>
>
> On Friday, 12 September 2014, 16:52, Joseph Bergmark
> <be...@apache.org> wrote:
>
>
>
>     It may also be worth mentioning that at injection time for normal
>     scoped beans we only inject a client proxy.  It may be the
>     construction of the proxy class that is hitting your no-args
>     constructor.  
>
>     Once you invoke a method on that proxy, I would expect the @Inject
>     constructor to be called when the proxy obtains an instance form
>     the context.
>
>     On Fri, Sep 12, 2014 at 10:24 AM, Romain Manni-Bucau
>     <rmannibucau@gmail.com <ma...@gmail.com>> wrote:
>
>         Hi
>
>         did you test against 1.7? code was totally rewritten
>
>
>         Romain Manni-Bucau
>         Twitter: @rmannibucau
>         Blog: http://rmannibucau.wordpress.com/
>         LinkedIn: http://fr.linkedin.com/in/rmannibucau
>         Github: https://github.com/rmannibucau
>
>
>         2014-09-12 15:56 GMT+02:00 Martin Strohal
>         <martin.strohal@1und1.de <ma...@1und1.de>>:
>         > Hi,
>         >
>         > I have a question about ApplicationScoped CDI beans with
>         constructor
>         > injection.
>         > My bean has two constructors: one default without parameters
>         and one
>         > with @Inject.
>         > If I inject this bean in another one, only the default
>         constructor is
>         > executed (I've put some logging messages in both
>         constructors). The
>         > injection constructor is not executed, so the field is null.
>         > How can that be? What am I doing wrong?
>         >
>         > (BTW: I'm using openwebbeans 1.1.8 in TomEE 1.5.2)
>         >
>         > Thanks,
>         > Martin
>
>
>
>


Re: Only no-args constructor executed

Posted by Mark Struberg <st...@yahoo.de>.
It's exactly as Joe already explained. Whenever we use client proxies in JavaEE (which is for *all* NormalScoped CDI beans plus all NoInterfaceView EJBs) we generate a dynamic subclass on the fly ('Contextual Reference') which overloads all the methods and delegates through to the 'Contextual Instance'

For a native class

public class User {
  private String name;
  public String getName() { return name; }
  public void setName(String name) {this.name=name;}
}

we will create a new class on the fly (see NormalScopedProxyFactory in our code) which is like:

public class User$$OwbNormalScopeProxy extends User {
  private User owbContextualInstanceProvider;

  @Override

  public String getName() { return owbContextualInstanceProvider.getName();};

  @Override
  public void setName(String name) { owbContextualInstanceProvider.setName(name); };
}

And of course this 'Contextual Reference' can only use the default constructor of the proxied class.
Btw, this is one of the reasons why you should always use @PostConstruct and not the ct to initialize your beans.

LieGrue,
strub



On Friday, 12 September 2014, 16:52, Joseph Bergmark <be...@apache.org> wrote:
 

>
>
>It may also be worth mentioning that at injection time for normal scoped beans we only inject a client proxy.  It may be the construction of the proxy class that is hitting your no-args constructor.  
>
>Once you invoke a method on that proxy, I would expect the @Inject constructor to be called when the proxy obtains an instance form the context.
>
>
>On Fri, Sep 12, 2014 at 10:24 AM, Romain Manni-Bucau <rm...@gmail.com> wrote:
>
>Hi
>>
>>did you test against 1.7? code was totally rewritten
>>
>>
>>Romain Manni-Bucau
>>Twitter: @rmannibucau
>>Blog: http://rmannibucau.wordpress.com/
>>LinkedIn: http://fr.linkedin.com/in/rmannibucau
>>Github: https://github.com/rmannibucau
>>
>>
>>
>>2014-09-12 15:56 GMT+02:00 Martin Strohal <ma...@1und1.de>:
>>> Hi,
>>>
>>> I have a question about ApplicationScoped CDI beans with constructor
>>> injection.
>>> My bean has two constructors: one default without parameters and one
>>> with @Inject.
>>> If I inject this bean in another one, only the default constructor is
>>> executed (I've put some logging messages in both constructors). The
>>> injection constructor is not executed, so the field is null.
>>> How can that be? What am I doing wrong?
>>>
>>> (BTW: I'm using openwebbeans 1.1.8 in TomEE 1.5.2)
>>>
>>> Thanks,
>>> Martin
>>
>
>
>

Re: Only no-args constructor executed

Posted by Joseph Bergmark <be...@apache.org>.
It may also be worth mentioning that at injection time for normal scoped
beans we only inject a client proxy.  It may be the construction of the
proxy class that is hitting your no-args constructor.

Once you invoke a method on that proxy, I would expect the @Inject
constructor to be called when the proxy obtains an instance form the
context.

On Fri, Sep 12, 2014 at 10:24 AM, Romain Manni-Bucau <rm...@gmail.com>
wrote:

> Hi
>
> did you test against 1.7? code was totally rewritten
>
>
> Romain Manni-Bucau
> Twitter: @rmannibucau
> Blog: http://rmannibucau.wordpress.com/
> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> Github: https://github.com/rmannibucau
>
>
> 2014-09-12 15:56 GMT+02:00 Martin Strohal <ma...@1und1.de>:
> > Hi,
> >
> > I have a question about ApplicationScoped CDI beans with constructor
> > injection.
> > My bean has two constructors: one default without parameters and one
> > with @Inject.
> > If I inject this bean in another one, only the default constructor is
> > executed (I've put some logging messages in both constructors). The
> > injection constructor is not executed, so the field is null.
> > How can that be? What am I doing wrong?
> >
> > (BTW: I'm using openwebbeans 1.1.8 in TomEE 1.5.2)
> >
> > Thanks,
> > Martin
>

Re: Only no-args constructor executed

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

did you test against 1.7? code was totally rewritten


Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau


2014-09-12 15:56 GMT+02:00 Martin Strohal <ma...@1und1.de>:
> Hi,
>
> I have a question about ApplicationScoped CDI beans with constructor
> injection.
> My bean has two constructors: one default without parameters and one
> with @Inject.
> If I inject this bean in another one, only the default constructor is
> executed (I've put some logging messages in both constructors). The
> injection constructor is not executed, so the field is null.
> How can that be? What am I doing wrong?
>
> (BTW: I'm using openwebbeans 1.1.8 in TomEE 1.5.2)
>
> Thanks,
> Martin