Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ManagedProperty annotation returns null instead of injecting the property

In one sentence: the @ManagedProperty annotation does return null instead of injecting the property.

The details: there are two classes:

@ManagedBean(name="authFilter")
@SessionScoped
public class AuthFilter implements Filter {
    @ManagedProperty("#{loginBean}")
    private LoginBean loginBean;
    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }
    ...
}

and

@ManagedBean(name="loginBean")
@SessionScoped
public class LoginBean  {
    ...
}

Now, AFAIK the the @ManagedProperty annotation and the setter should be enough for the property to be injected, or at least the other questions here and BalusC's blog suggest this, but it still always remains null.

It is also interesting and probably is related to this issue, that I get warnings for these classes that they are already have been registered as a managed bean, even though they only have been registered via the faces-config.xml or with the annotataions. ( Tried them booth, separately, no difference.) If none of them is present, then the WARNINGs disappear, but the @ManagedProperty does not work ofc.

WARNING: JSF1074: Managed bean named 'confListBean' has already been registered. Replacing existing managed bean class type <projectname>.web.authFilter with <projectname>.web.authFilter.

So the questions would be:

  • What else is required for a @ManagedProperty to be injected what I am not doing?
  • What possibly can 'overregister' these managed beans besides the @notations and faces-config.xml? Is there a trace for the process which collects the managed beans I could check?
like image 506
Alex Biro Avatar asked Nov 12 '22 19:11

Alex Biro


1 Answers

When defining the loginBean, make sure you set the eager flag to true.

@ManagedBean(name="loginBean", eager=true)

This will ensure that the loginBean is created, even if it is not referenced from a GUI element.

like image 74
abu.marcose Avatar answered Dec 01 '22 07:12

abu.marcose