Vaadin Spring XmlWebApplicationContext 的插件 NotSerializableException 即使是瞬态 ApplicationContext UI 的成员

Vaadin Spring Addon NotSerializableException for XmlWebApplicationContext even with transient ApplicationContext UI's member

我正在将 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 class:

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 注释定义了一个名为 "userService" 的 Spring bean MyConfiguration class 内)。另请注意,我有一个成员 transient 字段引用了 Spring 的 ApplicationContext(我在 UI 的 getUserService(VaadinRequest ) 方法以获得 "userService" bean)。

这是 UserService 界面和 class 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;
    }

}

然后,用户 class:

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 并在不使用 [=29 时将其放在 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 的另一个实例并在内部使用它,是这样吗?那么:

有办法解决吗?怎么样?

感谢关注!

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

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

  1. Disable session persistence
  2. 使用另一个容器,如 Jetty,默认情况下不会保留会话

我认为问题来自 SpringAwareVaadinServlet。它将您的 applicationContext 注入 SpringAwareUIProvider 并有效地存储在持久会话存储中。

如果您真的希望使用此设置保持会话持久性,则需要扩展 VaadinServlet 并实现一个不存储 applicationContext 本身但从另一个地方获取的自定义 UIProvider ,可能是您的 servlet 中的静态字段。