why NPE in this bean?
I have a session bean:
<managed-bean>
<managed-bean-name>mainMenuNavigationBean</managed-bean-name>
<managed-bean-class>com.cloud.common.jsf.core.beans.MainMenuNavigationBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
I inject this into a request bean:
<managed-bean>
<managed-bean-name>createAccountBean</managed-bean-name>
<managed-bean-class>com.cloud.common.jsf.account.CreateAccountBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>mainMenuNavigationBean</property-name>
<property-class>com.cloud.common.jsf.core.beans.MainMenuNavigationBean</property-class>
<value>#{mainMenuNavigationBean}</value>
</managed-property>
</managed-bean>
Now, I'm trying to use a setter in @PostConstruct of my createAccountBean but I do not understand why mainMenuNavigationBean is null here... I expect it to be autocreated if null, when accessing createAccountBean
@PostConstruct
public void init() {
userLoginVo = new UserLoginVo();
//NPE here
开发者_运维问答 mainMenuNavigationBean.setExternalPage(true);
}
Can you give me a hint please? I can't understand what I am doing wrong...
In order to get <managed-property>
to work properly, you need to ensure that the to-be-injected bean is a public
class with a (implicit) public
default constructor which doesn't throw any exception upon construction.
public class MainMenuNavigationBean {
public MainMenuNavigationBean() {
// Can even be omitted altogether if you don't have other constructors.
}
// ...
}
And you need to ensure that the acceptor has a valid property and a working setter for that.
public class CreateAccountBean {
private MainMenuNavigationBean mainMenuNavigationBean;
public void setMainMenuNavigationBean(MainMenuNavigationBean mainMenuNavigationBean) {
this.mainMenuNavigationBean = mainMenuNavigationBean;
}
// ...
}
Mind the this
in the setter, if you omit that, the setter has no effect.
精彩评论