Spring session-scoped beans (controllers) and references to services, in terms of serialization

前端 未结 6 1165
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 12:44
  • a standard case - you have a controller (@Controller) with @Scope(\"session\").
  • classes put in the session usually are expected to i
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 12:47

    Serialization of Dynamic-Proxies works well, even between different JVMs, eg. as used for Session-Replication.

    @Configuration public class SpringConfig {
    @Bean 
    @Scope(proxyMode = ScopedProxyMode.INTERFACES) 
    MyService myService() {
        return new MyService();
    }
    .....
    

    You just have to set the id of the ApplicationContext before the context is refreshed (see: org.springframework.beans.factory.support.DefaultListableBeanFactory.setSerializationId(String))

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    // all other initialisation part ...
    // before! refresh
    ctx.setId("portal-lasg-appCtx-id");
    // now refresh ..
    ctx.refresh();
    ctx.start();
    

    Works fine on Spring-Version: 4.1.2.RELEASE

提交回复
热议问题