从实例范围的bean自动实例化会话范围的bean [英] Auto-instantiate session-scoped bean from view-scoped bean
问题描述
每次尝试将会话范围的bean注入视图范围的bean时,在调用上述bean时都会收到NullPointerException.此问题直接与自动-实例化会话bean?
Every time I try to inject a session-scoped bean into my view-scoped beans, I get a NullPointerException when calling said bean. This problem is directly related to auto -instantiate a session bean?
这是我到目前为止尝试过的:
Here is what I tried so far:
faces-config.xml
faces-config.xml
<managed-bean>
<managed-bean-name>sessionBean</managed-bean-name>
<managed-bean-class>com.example.SessionBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>viewBean</managed-bean-name>
<managed-bean-class>com.example.ViewBean</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
<managed-property>
<property-name>sessionBean</property-name>
<property-class>com.example.SessionBean</property-class>
<value>#{sessionMBean}</value>
</managed-property>
</managed-bean>
SessionBean.java:
SessionBean.java:
package com.example;
public class SessionBean {
public SessionBean() {
System.out.println("Session is instantiated.");
}
public void sayHello() {
System.out.println("Hello from session");
}
}
ViewBean.java:
ViewBean.java:
package com.example;
public class ViewBean {
private SessionBean sessionBean;
private String text = "Look at me!";
public ViewBean() {
System.out.println("View bean is instantiated.");
sessionBean.sayHello();
}
public SessionBean getSessionBean() {
return sessionBean;
}
public void setSessionBean(SessionBean sessionBean) {
this.sessionBean = sessionBean;
}
public String getText() {
return text;
}
}
和index.xhtml的相关内容:
and the relevant content of index.xhtml:
<f:view>
<h:outputText value="#{viewBean.text}"/>
</f:view>
这就是我得到的:
com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.example.ViewBean.
...
Caused by: java.lang.NullPointerException
at com.example.ViewBean.(ViewBean.java:12)
这在weblogic-10.3.6上运行(或什至不运行),附带的jsf-2-0.war作为库部署.
This runs (or rather doesn't run) on weblogic-10.3.6 with the shipped jsf-2-0.war deployed as a library.
我在这里做错了什么?我希望这不是容器错误...
What am I doing wrong here? I'm hoping this is not a container bug...
推荐答案
您不能在@ViewScoped
构造函数中访问@SessionScoped
bean.在调用@ViewScoped
bean的构造函数之后,将设置@SessionScoped
bean.
在某种 init 方法中使用@PostConstruct
批注来访问@SessionScoped
bean.
You cannot access the @SessionScoped
bean in the @ViewScoped
constructor. The @SessionScoped
bean will be set AFTER the constructor of the @ViewScoped
bean has been called.
Use the @PostConstruct
annotation in some kind of init method to access the @SessionScoped
bean.
public ViewBean() {
System.out.println("Constructor viewbean");
}
@PostConstruct
public void init() {
sessionBean.sayHello();
}
其他链接:
为什么使用@PostConstruct?
Spring注入-在构造函数中访问注入的对象
Further Links:
Why use @PostConstruct?
Spring Injection - access to the injected object within a constructor
这篇关于从实例范围的bean自动实例化会话范围的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!