据我所知,FacesContext 仅在请求范围内可用。我创建了一个尝试接收 FacesContext 实例的线程,但它返回 null。
我的观点是每 10 秒更新一些应用程序范围的 bean。
线程的运行方法:
@Override
public void run()
{
while (true)
{
try
{
TimeView timeView = (TimeView)FacesContext.getCurrentInstance().
getExternalContext().getApplicationMap().get("timeView");
// FacesContext.getCurrentInstalce() returns null
timeView.update();
Thread.sleep(10000);
}
catch (InterruptedException ex)
{
System.out.println(ex);
}
}
}
TimeView 的标题(我跳过了 getter/setter):
@ManagedBean(eager=true, name="timeView")
@ApplicationScoped
public class TimeView implements Serializable
{
private int hour;
private int minutes;
private int seconds;
public TimeView()
{
update();
}
public void update()
{
Date date = new Date();
setHour(date.getHours());
setMinutes(date.getMinutes());
setSeconds(date.getSeconds());
}
面孔-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>timeView</managed-bean-name>
<managed-bean-class>foogame.viewBeans.TimeView</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
</faces-config>
那么,有没有办法在这个线程中接收对我的应用程序范围 bean 的引用?