{"id":4756,"date":"2014-03-30T15:08:15","date_gmt":"2014-03-30T15:08:15","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/tomcat-session-not-serializable-for-spring-component-with-value-injection-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:08:15","modified_gmt":"2014-03-30T15:08:15","slug":"tomcat-session-not-serializable-for-spring-component-with-value-injection-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/tomcat-session-not-serializable-for-spring-component-with-value-injection-collection-of-common-programming-errors\/","title":{"rendered":"tomcat session not serializable for spring component with @Value injection-Collection of common programming errors"},"content":{"rendered":"<p>It&#8217;s a rather strange problem. In Spring I&#8217;ve configured a bean as session.<\/p>\n<pre><code>@Component\n@Scope(value=\"session\", proxyMode=ScopedProxyMode.TARGET_CLASS)\npublic class UserSession implements Serializable{...}\n<\/code><\/pre>\n<p>There is an interceptor which who interact with this session.<\/p>\n<pre><code>public class UserInterceptor extends HandlerInterceptorAdapter{\n    ...\n    @Autowired private UserSession userSession;\n    @Autowired private WebUser webUser;\n\n    public boolean preHandle(...){\n        userSession.setUserId(webUser.getUserId());\n        userSession.setUserList(webUser.getUsers());\n        ...\n    }\n...\n}\n<\/code><\/pre>\n<p>The <code>WebUser<\/code> is an interface, in which depend on environment it will inject different <code>WebUser<\/code>. In profile <code>LOCAL<\/code>, in will inject <code>LocalWebUser<\/code><\/p>\n<pre><code>@Component\n@Primary\n@Profile({\"LOCAL\"})\npublic class LocalWebUser implements WebUser, Serializable{\n   @Autowired private transient UserManager userManager;\n\n   @Value(\"${env.tester.userId}\")\n   private transient String userId;\n\n   @Override\n   public List getUsers() {\n       return this.userManager.getUsers();\n   }\n   ...\n}\n<\/code><\/pre>\n<p>Notice that I did give make field as <code>transient<\/code> to prevent common serialization problem. However, in tomcat it throws that <code>LocalWebUser<\/code> is not serializable.<\/p>\n<blockquote>\n<blockquote>\n<p>Caused by: java.io.NotSerializableException: com.company.component.web.LocalWebUser$1 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) &#8230;.<\/p>\n<\/blockquote>\n<\/blockquote>\n<p><code>LocalWebUser<\/code> is actually <strong>not stored in session<\/strong>, it&#8217;s just that session retrieve value from it. Why it complains about it is not serializable?<\/p>\n<p>I did found out a solution that by <strong>removing <code>@Value<\/code> injection<\/strong> from properties, the exception went away, which confuse me even more&#8230;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s a rather strange problem. In Spring I&#8217;ve configured a bean as session. @Component @Scope(value=&#8221;session&#8221;, proxyMode=ScopedProxyMode.TARGET_CLASS) public class UserSession implements Serializable{&#8230;} There is an interceptor which who interact with this session. public class UserInterceptor extends HandlerInterceptorAdapter{ &#8230; @Autowired private UserSession userSession; @Autowired private WebUser webUser; public boolean preHandle(&#8230;){ userSession.setUserId(webUser.getUserId()); userSession.setUserList(webUser.getUsers()); &#8230; } &#8230; } The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4756","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4756"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4756\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}