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 {
ois.defaultReadObject();
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.
transient SampleService ss;
\$\endgroup\$