1

I'm trying to get a session scope managedbean in this way:

@ManagedBean
@SessionScoped
@SuppressWarnings("serial")
public class LoginBean implements Serializable{
......
}

@ManagedBean
@SessionScoped
public class MenuBean implements Serializable{
@ManagedProperty(value="#{loginBean}")
private LoginBean login;      
public void setLogin(LoginBean login) {
this.login = login;
}

public LoginBean getLogin() {
return login;
}

...
}  

But MenuBean loginBean is always null, what can I be doing wrong? can you help me please?

Thanks in advance.

Regards.

Mariah
  • 1,073
  • 1
  • 14
  • 33
  • 3
    First make sure you're importing your scoping annotations from the `javax.faces.*` package. Then also confirm `LoginBean` is actually being instantiated (i.e. no initialization-related exceptions in the logs etc) – kolossus Oct 11 '14 at 03:47
  • Hello, yes my import look like this: import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.SessionScoped; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; also try to get the bean in session like this: FacesContext context = FacesContext.getCurrentInstance(); LoginBean login= (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class); if I do it this way I can get the bean with ManagedProperty I cant Thx 4 ur help – Mariah Oct 13 '14 at 14:24
  • At what point are you trying to access `loginBean` and you're getting null? Not the constructor yeah? – kolossus Oct 13 '14 at 17:08
  • im trying to get loginBean from the constructor of MenuBean, is this a bad way to do it? – Mariah Oct 13 '14 at 18:26
  • Yes actually. Injected properties are not available as at then. I'll explain in an answer – kolossus Oct 13 '14 at 18:46
  • 1
    Ok, i supposed that i can do it using @PostConstruct rigth? – Mariah Oct 13 '14 at 19:04

1 Answers1

1

Injection of resources happens immediately after successful construction and as a result, you're not going to be able to use injected fields before or during construction of the bean. Hence, the need for @PostConstruct. Injected properties are available for use in the @PostConstruct method (which executes immediately after construction, the Post in @PostConstruct)

Related

Community
  • 1
  • 1
kolossus
  • 20,559
  • 3
  • 52
  • 104