Tobias Barth
Tobias Barth

Reputation: 76

transient Service @Reference in Sling Servlet

I have a couple of Sling servlets in a project (implementing SlingAllMethodsServlet or SlingSafeMethodsServlet), and some of them reference Sling services:

public class MyServlet extends SlingAllMethodsServlet{
    @Reference
    private SlingSettingsService slingSettingsService;

}

Now, static code analyzers like Sonar tell me to make the variable "slingSettingsService" transient or Serializable, because a Sling Servlet implements Serializable:

public class MyServlet extends SlingAllMethodsServlet{
    @Reference
    private transient SlingSettingsService slingSettingsService;
}

As an alternative, they tell me to make the referenced service implement Serializable, but that seems to be impossible to me. Is that a good idea to make the reference transient? In which case will a Sling servlet ever be serialized and deserialized, and will the trasient referenced be recovered then?

Upvotes: 4

Views: 1368

Answers (1)

user8723658
user8723658

Reputation: 51

Its pretty much a baggage from servlet api. AFAIK sling servlets are never serialized. Also, due to service objects being managed by scr, any osgi component with service references is unlikely to survive serialization and deserialization. you should ignore that sonar rule.

Upvotes: 1

Related Questions