用于XmlWebApplicationContext的Vaadin Spring Addon NotSerializableException,即使是瞬态ApplicationContext UI的成员

时间:2015-02-27 14:13:45

标签: java spring serialization vaadin

我正在将Spring整合到Vaadin中,感谢Vaadin Spring插件和这个wiki:https://vaadin.com/wiki?p_p_id=36&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&p_p_col_id=row-1&p_p_col_pos=2&p_p_col_count=4&_36_struts_action=%2Fwiki%2Fview&p_r_p_185834411_nodeName=vaadin.com+wiki&p_r_p_185834411_title=I+-+Getting+started+with+Vaadin+Spring我实际上能够做到。

然而,当我重新启动Tomcat并重新启动Vaadin应用程序时,我得到以下NotSerializableException抱怨Spring的XmlWebApplicationContext

SEVERE: Exception loading sessions from persistent storage
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: org.springframework.web.context.support.XmlWebApplicationContext
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1355)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    at java.util.LinkedList.readObject(LinkedList.java:1149)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1896)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
    at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:501)
    at com.vaadin.server.VaadinSession.readObject(VaadinSession.java:1443)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1896)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    at org.apache.catalina.session.StandardSession.doReadObject(StandardSession.java:1634)
    at org.apache.catalina.session.StandardSession.readObjectData(StandardSession.java:1099)
    at org.apache.catalina.session.StandardManager.doLoad(StandardManager.java:261)
    at org.apache.catalina.session.StandardManager.load(StandardManager.java:180)
    at org.apache.catalina.session.StandardManager.startInternal(StandardManager.java:460)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5213)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.NotSerializableException: org.springframework.web.context.support.XmlWebApplicationContext
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at java.util.LinkedList.writeObject(LinkedList.java:1131)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at org.apache.catalina.session.StandardSession.doWriteObject(StandardSession.java:1710)
    at org.apache.catalina.session.StandardSession.writeObjectData(StandardSession.java:1116)
    at org.apache.catalina.session.StandardManager.doUnload(StandardManager.java:401)
    at org.apache.catalina.session.StandardManager.unload(StandardManager.java:320)
    at org.apache.catalina.session.StandardManager.stopInternal(StandardManager.java:487)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5409)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
    at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1425)
    at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1414)
    ... 4 more

实际上代码非常简单(基于教程和其他一些Spring功能):

这是UI类:

package com.example.vaadinwithspring;

//...

@SpringUI("")
@Theme("valo")
@SuppressWarnings("serial")
public class MyUI extends UI {

    private transient ApplicationContext appContext;

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();
        layout.setMargin(true);
        setContent(layout);

        UserService service = getUserService(vaadinRequest);
        User user = service.getUser();
        String userName = user.getName();
        Label userHelloLabel = new Label("Hello " + userName + "!");

        layout.addComponent(userHelloLabel);

    }

    @WebListener
    public static class MyContextLoaderListener extends ContextLoaderListener {
    }

    @Configuration
    @EnableVaadin
    public static class MyConfiguration {

        @Bean(name="userService")
        public UserService helloWorld() {
            return new UserServiceImpl();
        }

    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends SpringAwareVaadinServlet {
    }

    private UserService getUserService(VaadinRequest request) {
        WrappedSession session = request.getWrappedSession();
        HttpSession httpSession = ((WrappedHttpSession) session).getHttpSession();
        ServletContext servletContext = httpSession.getServletContext();
        appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

        return (UserService) appContext.getBean("userService");
    }

}

虽然非常混乱(我遵循了wiki),但这里唯一重要的事情是Vaadin Spring注释(@SpringUI("")@EnableVaadin,如链接教程)和一些Spring的注释(除了@Configuration之外,我还通过@Bean类中的MyConfiguration注释定义了一个名为“userService”的 Spring bean 。还请注意,我有一个成员 transient 字段,它引用了Spring的ApplicationContext(我在UI的getUserService(VaadinRequest)方法中使用它以获取“userService”bean)。

以下是UserService界面和课程UserServiceImpl的代码:

package com.example.vaadinwithspring;

public class UserService {

    public User getUser();

}

-------------------------------------------------------------------

package com.example.vaadinwithspring;

public class UserServiceImpl implements UserService {

    @Override
    public User getUser() {
        User user = new User();
        user.setName("UserName");
        return user;
    }

}

然后,User类:

package com.example.vaadinwithspring;

public class User {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

所有组件都是虚拟实现,只是为了看到Vaadin与Spring合作并且一切正常。

最后, Vaadin Spring 教程建议使用applicationContext.xml并在不使用Spring Boot or JavaConfig时将其放在src / main / webapp / WEB-INF /中。

我创建了以下applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <bean class="com.example.vaadinwithspring.MyUI.MyConfiguration" />
    <context:component-scan base-package="com.example.vaadinwithspring" />
</beans>

现在,正如我所说,如果我启动应用程序,一切都很有效。我遇到的唯一问题是,当我重新启动应用程序时NotSerializableExceptionXmlWebApplicationContext相关。

如果我有一个不应序列化的瞬态ApplicationContext成员,为什么会抛出异常呢?我想Spring使用XmlWebApplicationContext的另一个实例并在内部使用它,是这样吗?然后:

有没有办法解决它?怎么样?

感谢您的关注!

1 个答案:

答案 0 :(得分:3)

默认情况下,Tomcat尝试在重新启动时加载上一次运行的会话。如果加载那些会话(您的情况)失败,它会将错误打印到日志,然后正常初始化您的应用程序。实际上,这里没有问题,但是你没有得到会话持久性。除非您使用的是生产应用,否则您不需要此功能。

除了忽略它之外,您可以采取一些方法:

  1. Disable session persistence
  2. 使用其他容器,例如Jetty,默认情况下不会保留会话
  3. 我认为问题来自SpringAwareVaadinServlet。它将applicationContext注入SpringAwareUIProvider,并有效地存储在持久会话存储中。

    如果你真的想要使用此设置进行会话持久性,则需要扩展VaadinServlet并实现一个不存储UIProvider本身的自定义applicationContext,但它可以从其他地方获取,可能servlet中的静态字段。