0

我正在使用带有 EJB 3 的 Wicket,当我调用我的页面时,日志显示错误

Error serializing object class com.mk.view.page.CountryList [object=[Page class = com.mk.view.page.CountryList, id = 91, render count = 1]]
org.apache.wicket.core.util.objects.checker.CheckingObjectOutputStream$ObjectCheckException: The object type is not Serializable!
A problem occurred while checking object with type: javax.naming.InitialContext
Field hierarchy is:
  91 [class=com.mk.view.page.CountryList, path=91]
    private javax.naming.Context com.mk.view.page.CountryList.ctx [class=javax.naming.InitialContext] <----- field that is causing the problem

我的代码是

public class CountryList extends Layout {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * 
     */


    private javax.naming.Context ctx;
    private GenericCrudService sf;



    private CountryList(){      
        try {

                ctx = new javax.naming.InitialContext();
                sf = (GenericCrudService) ctx
                        .lookup("java:global/mkEar/mkEJB/CrudService!com.mk.business.common.GenericCrudService");

        } catch (NamingException e) {
            e.printStackTrace();
        }

        addModelModule();
        addSearchModule();
    }

我的应用程序没有崩溃,我还没有找到解决方案,所以我想知道这些是否会比日志更糟糕?有谁知道解决这个问题?

4

2 回答 2

1

不要将引用保留GenericCrudService为字段(也不要作为InitialContext实例)。将查找代码重构为方法,并在每次需要服务时检索它。您可以重用它,但将其保留在本地(变量)或请求范围内。

AFAIK,不能保证从 JNDI 查找返回的 EJB 将是可序列化的,即使它们实现了接口并遵循序列化规则。容器可能会返回一个代理,而不是直接返回对象实例。

Since Wicket will serialize stateful pages after requests, they can't have non-serializable attributes. This is the cause of the error you're getting.

于 2013-01-03T22:11:13.627 回答
0

You can also mark your GenericCrudService field transient and initialize instance in Component onAttach/onConfigure method. When using Spring there is a dedicated annotation @SpringBean in Wicket, which will make your non-serializable service field a proxy and handle instance setting for you.

于 2013-01-06T09:22:17.607 回答