Getting java.io.NotSerializableException with a spring service when stopping tomcat

后端 未结 4 1266
遇见更好的自我
遇见更好的自我 2021-01-13 11:11

i am using spring 3 with JSF 2 and i replaced JSF managed beans with spring beans, by adding on top of bean:

@Component(\"mybean\")
@Scope(\"session\")
         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 11:50

    I had to use readObject() + static ApplicationContext hack to solve the issue:

    @Component
    @Scope(value = SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
    public class FlashMessages implements Serializable {    
    
        @Autowired
        transient private SomeBean someBean;
    
        private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
            ois.defaultReadObject();
            // restore someBean field on deserialization
            SpringUtils.getContext().getAutowireCapableBeanFactory().autowireBean(this);
        }
    
    }
    
    @Component
    public class SpringUtils implements ApplicationContextAware {
    
        static ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            SpringUtils.applicationContext = applicationContext;
        }
    
        public static ApplicationContext getContext() {
            return applicationContext;
        }
    
    }
    

提交回复
热议问题