3

I have problem with JSF beans using Spring managed services. I got an error telling, that spring bean used in JSF bean is not serializable.

@ManagedProperty("#{customerService}")
private CustomerService customerService;

I can't make the service serializable, because it is using JdbcTemplate which itself isn't serializable. Moreover, serializing Spring beans which have application scope makes no sense at all, so I don't understand, why someone's code is attempting to serialize them.

I have worked with JSF project using Spring services, and there were no such issues, so such cooperation must be possible. But this project is made from scratch based on example projects, so there must be something wrong with the configuration of spring-JSF cooperation, but I don't know where to search.

The configuration of Spring for JSF is:

<!-- JSF and Spring are integrated -->
<application>
    <el-resolver>
        org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
</application>

How to solve this issue?

Danubian Sailor
  • 1
  • 38
  • 145
  • 223
  • Is there any particular reason to use Spring instead of EJB/JPA? – BalusC Nov 21 '12 at 16:23
  • Many reasons, Spring is more wide and mature, Spring works without problem on Jetty or Tomcat and I know Spring much better. And have EJB something similar to Spring MVC? – Danubian Sailor Nov 21 '12 at 16:26
  • 1
    Uh, Spring is more than alone Spring MVC. Spring MVC's counterpart in Java EE is JSF itself which you're already using. Spring also offers a service layer framework which is by Java EE provided in flavor of EJB. EJB is not a MVC framework at all. Spring was more popular during the dark J2EE ages because EJB2 was extemely terrible. Since EJB3 a lot of lessons from Spring were incorporated which made it much more simple than EJB2 and even Spring itself. As to Jetty/Tomcat, just look at OpenEJB and/or TomEE. – BalusC Nov 21 '12 at 16:34
  • It's more about that I'm using spring to create REST JSON channel and provide images generated from database etc. – Danubian Sailor Nov 21 '12 at 16:38
  • For that, JAX-RS (e.g. [Jersey](http://jersey.java.net/nonav/documentation/latest/getting-started.html)) is provided by standard Java EE pack. – BalusC Nov 21 '12 at 16:39
  • So is your answer, that I should choose before throwing away spring and JSF? There must be some way... – Danubian Sailor Nov 21 '12 at 16:42
  • I don't know. I never really used Spring. I was just wondering. It's after all your choice. – BalusC Nov 21 '12 at 17:01
  • So far been using the solution posted here....[jsf-session-scoped-managed-bean-does-not-have-dependencies-re-injected-on-sess](http://stackoverflow.com/questions/3778353/jsf-session-scoped-managed-bean-does-not-have-dependencies-re-injected-on-sess) also as suggested by BalusC use Application#evaluateExpressionGet() for simplicity. – Ravi Kadaboina Nov 21 '12 at 18:22
  • My feelings toward Spring are similar to those: http://stackoverflow.com/a/71582/531954 – Danubian Sailor Nov 22 '12 at 11:01
  • @lechlukasz, just mark the DAO bean as `transient` in your managed bean and the context will skip it during serialization, or set your `STATE_SAVING_MODE` to client. @BalusC, there are still a lot of things less painful in spring than in EJBS e.g. JMS, AOP, Scheduling is still more powerful in Spring :). – kolossus Nov 22 '12 at 12:04

1 Answers1

3

There is no way to avoid JSF serialization mist. Even ApplicationScoped beans are serialized (when they are injected into other beans).

But the solution was made on the Spring side. You have to use scoped proxy.

To wrap the bean into serializable proxy you have to add to bean body:

<aop:scoped-proxy proxy-target-class="true"/>

The spring aop namespace and spring-aop dependency must be added.

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

And this is it! In the bean will be the serializable element, the proxy that will re-load bean from Spring context on deserialization.

The only mist here is that I have to create cglib class-level-proxy. JRE proxy was not working because the interface was not available during deserialization... I don't understand fully why but I have working solution at least.

Danubian Sailor
  • 1
  • 38
  • 145
  • 223
  • Hi, this hasn't worked for me.. besides adding that configuration, do I have to make the service implement serializable? or should that configuration be enough? – damian Nov 07 '13 at 00:57
  • 1
    No, the aim of proxy is to enable serialization of beans which are self not serializable. When deserializing, the bean is re-injected from spring context. – Danubian Sailor Nov 07 '13 at 06:06