New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add missing @Singleton annotation to templates controllers #6315
Conversation
@naferx I don't see any reason why these need to be singleton. They don't hold any internal state. |
import views.html.*; | ||
|
||
/** | ||
* This controller contains an action to handle HTTP requests | ||
* to the application's home page. | ||
*/ | ||
@Singleton |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmethvin You're right. They don't hold any internal state.
But I added it here by consistency because https://github.com/playframework/playframework/blob/master/templates/play-scala/app/controllers/HomeController.scala contains the annotation.
So, singleton in HomeController.scala
must be removed ? or added to HomeController.java
?
WDYT. Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, my general rule is to leave it off unless there is a good reason to do so. In HomeController's case I'd expect creating a new object to be faster than going through the Singleton scope logic to get the instance.
Assuming you keep your controllers stateless, there usually would be little or no benefit to using the Singleton scope, so I think leaving it off by default is a good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this comment, @jroper states that because the router is a singleton, "by association, the controllers it invokes are singleton". So, the singleton annotation is unnecessary here unless you are using the @
syntax at your routes
file. From the docs:
The injected routes generator also supports the @ operator on routes, but it has a slightly different meaning (since everything is injected), if you prefix a controller with @, instead of that controller being directly injected, a JSR 330 Provider for that controller will be injected. This can be used, for example, to eliminate circular dependency issues, or if you want a new action instantiated per request.
Maybe a better version of this PR is remove these annotations and better document how controllers are created/injected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this should be documented. I wasn't really aware that controllers even without the singleton annotation are effective still singletons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@marcospereira You're right, in a typical Play app controllers would effectively be singleton. But you could possibly define your own router that isn't a singleton, or have multiple routers that referenced the same controller, in which case it would make a difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmethvin yep. :-)
My point is that instead of just add/remove annotations from the templates, we should instead document this behavior (when controllers are singletons and when they aren't).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we can mention that the Router is Singleton and has the controllers injected, so they are effectively singletons. It doesn't necessarily have to work that way, though; that's just a consequence of the current implementation. If a user wants to ensure Singleton-ness, I'd still recommend they add the annotation or explicitly bind it in the Singleton scope.
@gmethvin, @marcospereira & @mkurz Thanks for the feedback. |
Closing due to inactivity -- @naferx if you pick this up, the templates have moved to https://github.com/playframework/play-java-intro for example |
No description provided.