# Spring autowiring in managed beans with support for serialization - is this safe?

Im trying to solve two problems I see with JSF2 and Spring.

# First

• @Autowired in @ManagedBean does not work
• Nor does @ViewScoped with @Controller
• @ManagedProperty is not type safe and requires setter

# Second

• You can not serialize singleton spring beans without hassle
• Managed beans must be serializable so the container can serialize the session

So I enabled @Autowired in managed beans and fixed serialization with this common base class which all managed beans (at least @SessionScoped and @ViewScoped ones) derive from.

public abstract class Handler implements Serializable {
private static final long serialVersionUID = 1L;

@PostConstruct
private void init() {
Spring.appCtx.getAutowireCapableBeanFactory().autowireBean(this);
}

private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
Spring.appCtx.getAutowireCapableBeanFactory().autowireBean(this);
}
}


Handlers are now implemented like that:

@ManagedBean
@RequestScoped
public class Index extends Handler implements Serializable {
private static final long serialVersionUID = 1L;

@Autowired
SampleService ss;

public String hello() {
return ss.sayHello();
}
}


First tests seem fine, but I'm curious if I'm causing any problems I don't know of.

• You might consider making the service reference transient. transient SampleService ss; – cjstehno Mar 25 '13 at 12:40
• @cjstehno: Could you write your comment as an asnwer to help us killing a zombie? meta.codereview.stackexchange.com/a/1511/7076 – palacsint Feb 26 '14 at 19:35
• Sure. No problem. Done. – cjstehno Feb 26 '14 at 20:18
• You can make your Managed bean Extends 'SpringBeanAutowiringSupport' – Daouda Beye Jan 8 '17 at 18:12

1. Handler is too generic name for a class. Try to find something more descriptive (which doesn't force readers to check the implementation if they want to know what the class does), SerializationHandler, for example.

2. In Java classes could have only one superclass. If you make it an abstract superclass you can't have another one which might be cumbersome in some cases.

Inheritance is appropriate only in circumstances where the subclass really is a subtype of the superclass. In other words, a class B should extend a class A only if an “is-a” relationship exists between the two classes. If you are tempted to have a class B extend a class A, ask yourself the question: Is every B really an A? If you cannot truthfully answer yes to this question, B should not extend A. If the answer is no, it is often the case that B should contain a private instance of A and expose a smaller and simpler API: A is not an essential part of B, merely a detail of its implementation.

Source: Effective Java, Second Edition, Item 16: Favor composition over inheritance

I'd use composition with a helper class:

@ManagedBean
@RequestScoped
public class Index implements Serializable {
private static final long serialVersionUID = 1L;

@Autowired
SampleService ss;

public String hello() {
return ss.sayHello();
}

@PostConstruct
private void init() {
WireHelper.wireObject(this);
}

private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
WireHelper.wireObject(this);
}
}

public final class WireHelper {
public static void wireObject(Object object) {
Spring.appCtx.getAutowireCapableBeanFactory().autowireBean(this);
}
}

3. If you stay with the original implementation: you don't necessarily need implements Serializable here since Handler already implements that interface:

public class Index extends Handler implements Serializable {
...
}

• 1) Good idea but does not matter too much because every ManagedBean will implement Handler. 2) I don't know if you are familiar with jsf, but there will be multiple ManagedBeans (some call them handlers) per jsf page, which may get easily into hundreds in a big app. If you don't extend, you will have to reimplement readObject() every time. Too much duplication and very error prone (may be forgotten, hard to check). The solution we chose one year ago was simply to turn of session serialization in tomcat. Using @Scopes proxyMode works, too). – atamanroman Feb 26 '14 at 21:25
• @atamanroman: Thanks for the feedback! Just to make sure: "turn of" -> "turn off" - is it a typo? – palacsint Feb 26 '14 at 21:32
• Yes of course, sorry. Can't edit anymore, though. – atamanroman Feb 26 '14 at 22:10
• With Java 8 you can use default methods to achieve both! Interface without Code duplication :-) – Falco May 15 '15 at 7:57

You might consider making the service reference transient.

transient SampleService ss;