Spring Autowired & Serialization

Hi. I’m wondering what the best practice is regarding Vaadin components and autowiring services using Spring.

Vaadin components are Serializable and my services are not, so autowiring them leads to a code analysis warning. However Vaadin’s own sample and tutorial projects inject services with autowired, apparently ignoring the issue?
I’ve been using the approach suggested here: https://vaadin.com/forum/thread/17779205/vaadin-flow-spring-boot-using-autowired-in-components which works very well, but I’m wondering what the ‘official’ way to do this is.

What is the recommendation here, can I safely make the fields transient? Will Spring re-inject my beans when I serialize and deserialize the UI? Or do I actually need something like the static helper class that I’m using now?

I can’t find anything in the documentation about this which surprises me a bit, this feels like it should be a more common issue. Am I missing something? I searched on Discord too but I only found this with no replies: https://discord.com/channels/732335336448852018/774366825756229632/879366054801977415

Any help/thoughts are appreciated :slightly_smiling_face: My current solution is already working, I’m just wondering if there is a better way and why I can’t find any documentation about this.

Some thoughts: everything in Vaadin is serializable, because in the case you really need to serialize your entire session, then Vaadin components and data structures must be serializable too. However, serializing the entire session is a very heavy operation, and you only should do it if you have a very serious requirement about it. Serialization and deserialization of that much data takes a ton of CPU power. In a vast majority of cases, it’s enough to have sticky sessions enabled in your cluster.

I have no idea how DI works when you’re deserializing things, but it sounds like something that could be documented somewhere on the Spring side of things.

Also, if you really need to serialize your entire session, you need to write all your application code (including local data structures and UI code) in a way that’s serializable too.

Have you tried the add-on linked in the forum post you mentioned?

Yea so you’re saying that by far most people never actually serialize their sessions so they just ignore this (or never even notice it)? That’s fair.
We’re not serializing anything either atm, it’s just the code quality complaining that having non-serializable fields in a serializable class is bad practice. Which I mean, it’s not wrong.

Well anyway you’ve answered my question, so thanks. :+1:

I understand serializing a session is very much an edge case but considering it means all Vaadin classes are serializable I’d expect something about it in the documentation. This blog post is very useful: https://vaadin.com/blog/session-replication-in-the-world-of-vaadin but I feel like a short version of it should be in the documentation, I guess under ‘advanced topics’. Even if it’s only to tell users what you’ve said here, that it should really only be a last resort.
Then again if you got more questions about it, it would already be there, so maybe it’s just me.

Thanks again and have a nice weekend!

No I haven’t! It looks like it should work though. Why?

I just thought autowired serializable service references would be what you’re looking for. Maybe I misread. There’s no official way (and Spring will definitely not automatically inject anything into your deserialized session), but I would agree that there should be one.