Alex Biro
Alex Biro

Reputation: 1237

@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:

Upvotes: 5

Views: 7403

Answers (1)

abu.marcose
abu.marcose

Reputation: 560

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.

Upvotes: 2

Related Questions