我不确定我做的是不是错了,或者我只是错过了某处的注释或配置项。情况是这样的:
我有一个 JSF 应用程序,它有一个名为 SessionData
的 session 范围 bean .这个 bean 在创建时注入(inject)了一个应用程序范围的 bean 引用(类型为 ApplicationData
)。首次创建 session 时,这可以正常工作。依赖注入(inject)是用 <managed-bean>
完成的faces-config.xml
中的元素文件如下所示:
<managed-bean>
<managed-bean-name>sessionData</managed-bean-name>
<managed-bean-class>my.package.SessionData</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>applicationData</property-name>
<property-class>my.package.ApplicationData</property-class>
<value>#{applicationData}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>applicationData</managed-bean-name>
<managed-bean-class>my.package.ApplicationData</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
因为拥有我的 SessionData
没有意义对象包括 ApplicationData
对象序列化时,我标记了ApplicationData
在我的 SessionData
中作为 transient 引用对象:
transient private ApplicationData applicationData;
在 Web 应用程序停止(在我的 Tomcat 6.x 容器中)并且 session 被序列化之前,一切都很好。当我重新启动应用程序并且 session 被反序列化时,我对 ApplicationData
的引用不会被 JSF 重新注入(inject)。我知道反序列化应该使 transient 字段没有值。 有没有办法向 JSF 发出信号,表明此 session 范围的对象需要在反序列化后重新设置其依赖项?
我使用 MyFaces JSF 1.2 和 Tomcat 6.0.26 作为我的 Web 应用程序容器。
最佳答案
虽然 Bozho 提供的解决方案可行,但我不想将代理对象引入当前未使用它们的应用程序中。我的解决方案不太理想,但可以完成工作。
我将 transient 场留在原地:
transient private ApplicationData _applicationData;
我还保留了 setter,以便 JSF 可以在第一次创建 SessionData
对象时初始设置引用:
public void setApplicationData(ApplicationData applicationData) {
_applicationData = applicationData;
}
我所做的更改是在 getter 方法中。 SessionData
对象中的方法现在需要停止直接访问 _applicationData
字段,而是通过 getter 获取引用。 getter 将首先检查空引用。如果为 null,则通过 FacesContext
获取托管 bean。此处的约束是 FacesContext
仅在请求的生命周期内可用。
/**
* Get a reference to the ApplicationData object
* @return ApplicationData
* @throws IllegalStateException May be thrown if this method is called
* outside of a request and the ApplicationData object needs to be
* obtained via the FacesContext
*/
private ApplicationData getApplicationData() {
if (_applicationData == null) {
_applicationData = JSFUtilities.getManagedBean(
"applicationData", // name of managed bean
ApplicationData.class);
if (_applicationData == null) {
throw new IllegalStateException(
"Cannot get reference to ApplicationData object");
}
}
return _applicationData;
}
如果有人关心,这里是我的 getManagedBean()
方法的代码:
/**
* <p>Retrieve a JSF managed bean instance by name. If the bean has
* never been accessed before then it will likely be instantiated by
* the JSF framework during the execution of this method.</p>
*
* @param managedBeanKey String containing the name of the managed bean
* @param clazz Class object that corresponds to the managed bean type
* @return T
* @throws IllegalArgumentException Thrown when the supplied key does
* not resolve to any managed bean or when a managed bean is found but
* the object is not of type T
*/
public static <T> T getManagedBean(String managedBeanKey, Class<T> clazz)
throws IllegalArgumentException {
Validate.notNull(managedBeanKey);
Validate.isTrue(!managedBeanKey.isEmpty());
Validate.notNull(clazz);
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext == null) {
return null;
}
Validate.notNull(facesContext.getApplication());
ELResolver resolver = facesContext.getApplication().getELResolver();
Validate.notNull(resolver);
ELContext elContext = facesContext.getELContext();
Validate.notNull(elContext);
Object managedBean = resolver.getValue(
elContext, null, managedBeanKey);
if (!elContext.isPropertyResolved()) {
throw new IllegalArgumentException(
"No managed bean found for key: " + managedBeanKey);
}
if (managedBean == null) {
return null;
} else {
if (clazz.isInstance(managedBean)) {
return clazz.cast(managedBean);
} else {
throw new IllegalArgumentException(
"Managed bean is not of type [" + clazz.getName() +
"] | Actual type is: [" + managedBean.getClass().getName()+
"]");
}
}
}
并且不要接听我的验证电话。开发完成后我会把它们拿出来! :)
关于java - JSF - session 范围的托管 bean 没有在 session 反序列化时重新注入(inject)的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3778353/