§Removing GlobalSettings
If you are keen to use dependency injection, we are recommending that you move out of your GlobalSettings
implementation class as much code as possible. Ideally, you should be able to refactor your code so that it is possible to eliminate your GlobalSettings
class altogether.
Next follows a method-by-method guide for refactoring your code. Because the APIs are slightly different for Java and Scala, make sure to jump to the appropriate subsection.
Note: If you haven’t yet read about dependency injection in Play, make a point to do it now. Follow the appropriate link to learn about dependency injection in Play with Java or Scala.
§Scala
-
GlobalSettings.beforeStart
andGlobalSettings.onStart
: Anything that needs to happen on start up should now be happening in the constructor of a dependency injected class. A class will perform its initialisation when the dependency injection framework loads it. If you need eager initialisation (because you need to execute some code before the application is actually started), define an eager binding. -
GlobalSettings.onStop
: Add a dependency toApplicationLifecycle
on the class that needs to register a stop hook. Then, move the implementation of yourGlobalSettings.onStop
method inside theFuture
passed to theApplicationLifecycle.addStopHook
. Read Stopping/cleaning-up for more information. -
GlobalSettings.onError
: Create a class that inherits fromHttpErrorHandler
, and move the implementation of yourGlobalSettings.onError
inside theHttpErrorHandler.onServerError
method. Read Error Handling for more information. -
GlobalSettings.onRequestReceived
: Create a class that inherits fromHttpRequestHandler
, and move the implementation of yourGlobalSettings.onRequestReceived
inside theHttpRequestHandler.handlerForRequest
method. Read Request Handlers for more information.
Be aware that if in yourGlobalSettings.onRequestReceived
implementation you are callingsuper.onRequestReceived
, then you should inherits fromDefaultHttpRequestHandler
instead ofHttpRequestHandler
, and replace all calls tosuper.onRequestReceived
withsuper.handlerForRequest
. -
GlobalSettings.onRouteRequest
: Create a class that inherits fromDefaultHttpRequestHandler
, and move the implementation of yourGlobalSettings.onRouteRequest
method inside theDefaultHttpRequestHandler.routeRequest
method. Read Request Handlers for more information. -
GlobalSettings.onRequestCompletion
: This method is deprecated, and it is no longer invoked by Play. Instead, create a custom filter that attaches anonDoneEnumerating
callback onto the returnedEnumerator
result. Read Scala Http Filters for details on how to create a http filter. -
GlobalSettings.onHandlerNotFound
: Create a class that inherits fromHttpErrorHandler
, and provide an implementation forHttpErrorHandler.onClientError
. Read Error Handling for more information.
Note thatHttpErrorHandler.onClientError
takes astatusCode
in argument, hence your implementation should boil down to:
if(statusCode == play.api.http.Status.NOT_FOUND) {
// move your implementation of `GlobalSettings.onHandlerNotFound` here
}
GlobalSettings.onBadRequest
: Create a class that inherits fromHttpErrorHandler
, and provide an implementation forHttpErrorHandler.onClientError
. Read Error Handling for more information.
Note thatHttpErrorHandler.onClientError
takes astatusCode
in argument, hence your implementation should boil down to:
if(statusCode == play.api.http.Status.BAD_REQUEST) {
// move your implementation of `GlobalSettings.onBadRequest` here
}
-
GlobalSettings.configure
andGlobalSettings.onLoadConfig
: Specify all configuration in your config file or create your own ApplicationLoader (see GuiceApplicationBuilder.loadConfig). -
GlobalSettings.doFilter
: Create a class that inherits fromHttpFilters
, and provide an implementation forHttpFilter.filters
. Read Http Filters for more information.
Also, mind that if your Global
class is mixing the WithFilters
trait, you should now create a Filter class that inherits from HttpFilters
, and place it in the empty package. Read here for more details.
§Java
-
GlobalSettings.beforeStart
andGlobalSettings.onStart
: Anything that needs to happen on start up should now be happening in the constructor of a dependency injected class. A class will perform its initialisation when the dependency injection framework loads it. If you need eager initialisation (for example, because you need to execute some code before the application is actually started), define an eager binding. -
GlobalSettings.onStop
: Add a dependency toApplicationLifecycle
on the class that needs to register a stop hook. Then, move the implementation of yourGlobalSettings.onStop
method inside thePromise
passed to theApplicationLifecycle.addStopHook
. Read Stopping/cleaning-up for more information. -
GlobalSettings.onError
: Create a class that inherits fromHttpErrorHandler
, and move the implementation of yourGlobalSettings.onError
inside theHttpErrorHandler.onServerError
method. Read Error Handling for more information. -
GlobalSettings.onRequest
: Create a class that inherits fromDefaultHttpRequestHandler
, and move the implementation of yourGlobalSettings.onRequest
method inside theDefaultHttpRequestHandler.createAction
method. Read Request Handlers for more information. -
GlobalSettings.onRouteRequest
: There is no simple migration for this method when using the Java API. If you need this, you will have to keep your Global class around for a little longer. -
GlobalSettings.onHandlerNotFound
: Create a class that inherits fromHttpErrorHandler
, and provide an implementation forHttpErrorHandler.onClientError
. Read Error Handling for more information.
Note thatHttpErrorHandler.onClientError
takes astatusCode
in argument, hence your implementation should boil down to:
if(statusCode == play.mvc.Http.Status.NOT_FOUND) {
// move your implementation of `GlobalSettings.onHandlerNotFound` here
}
GlobalSettings.onBadRequest
: Create a class that inherits fromHttpErrorHandler
, and provide an implementation forHttpErrorHandler.onClientError
. Read Error Handling for more information.
Note thatHttpErrorHandler.onClientError
takes astatusCode
in argument, hence your implementation should boil down to:
if(statusCode == play.mvc.Http.Status.BAD_REQUEST) {
// move your implementation of `GlobalSettings.onBadRequest` here
}
-
GlobalSettings.onLoadConfig
: Specify all configuration in your config file or create your own ApplicationLoader (see GuiceApplicationBuilder.loadConfig). -
GlobalSettings.filters
: Create a class that inherits fromHttpFilters
, and provide an implementation forHttpFilter.filters
. Read Http Filters for more information.
Next: Migrating Anorm
Found an error in this documentation? The source code for this page can be found here. After reading the documentation guidelines, please feel free to contribute a pull request. Have questions or advice to share? Go to our community forums to start a conversation with the community.