Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

naferx
Copy link
Contributor

@naferx naferx commented Jul 11, 2016

No description provided.

@gmethvin
Copy link
Member

@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
Copy link
Contributor Author

@naferx naferx Jul 12, 2016

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

Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

@mkurz mkurz Jul 12, 2016

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.

Copy link
Member

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.

Copy link
Member

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).

Copy link
Member

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.

@naferx
Copy link
Contributor Author

naferx commented Jul 15, 2016

@gmethvin, @marcospereira & @mkurz Thanks for the feedback.
I'll work on it over the weekend

@wsargent
Copy link
Member

Closing due to inactivity -- @naferx if you pick this up, the templates have moved to https://github.com/playframework/play-java-intro for example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants