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\")
Well, finally i was able to make it work fine as follows:
1- The Service:
@Service
@Scope("singleton")
public class PersonService{
}
2- The Spring Managed Bean:
@Component("person")
@Scope("session")
public class PersonBean implements Serializable{
@Inject
private PersonService personService;
}
waiting for your feedback.
Looking at the error, It looks like tomcat is trying to serialize the entire spring context, I think it might be due to the fact that your are implementing Serializable on Service. I was thinking may be you need to do this, I don't know JSF that well but my understanding of scoped beans makes me think, You might need some thing like this
@Component
@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS, value="session")
public class MyBean implements Serializable
{
//no need to implement serializable on service
@Autowired(required=true)
private MyService service;
}
You get this exception because the class does not implement Serializable
. Tomcat serializes currently running HttpSession
objects including all its attributes to disk whenever it is about to restart/redeploy, so that they are revived after the cycle. Session scoped objects are stored as attributes of the HttpSession
, hence they need to implement Serializable
to survive the restart/redeploy as well.
If you would like to make them Serializable
, then implement it accordingly:
public class YourSpringService implements Serializable {}
Otherwise, just ignore the exception or turn off serializing sessions to disk in Tomcat's config.
Note that the same story also applies to JSF view/session scoped beans.
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;
}
}