§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.beforeStartandGlobalSettings.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 initialization when the dependency injection framework loads it. If you need eager initialization (because you need to execute some code before the application is actually started), define an eager binding. -
GlobalSettings.onStop: Add a dependency toApplicationLifecycleon the class that needs to register a stop hook. Then, move the implementation of yourGlobalSettings.onStopmethod inside theFuturepassed to theApplicationLifecycle.addStopHook. Read Stopping/cleaning-up for more information. -
GlobalSettings.onError: Create a class that inherits fromHttpErrorHandler, and move the implementation of yourGlobalSettings.onErrorinside theHttpErrorHandler.onServerErrormethod. Read Error Handling for more information. -
GlobalSettings.onRequestReceived: Create a class that inherits fromHttpRequestHandler, and move the implementation of yourGlobalSettings.onRequestReceivedinside theHttpRequestHandler.handlerForRequestmethod. Read Request Handlers for more information.
Be aware that if in yourGlobalSettings.onRequestReceivedimplementation you are callingsuper.onRequestReceived, then you should inherits fromDefaultHttpRequestHandlerinstead ofHttpRequestHandler, and replace all calls tosuper.onRequestReceivedwithsuper.handlerForRequest. -
GlobalSettings.onRouteRequest: Create a class that inherits fromDefaultHttpRequestHandler, and move the implementation of yourGlobalSettings.onRouteRequestmethod inside theDefaultHttpRequestHandler.routeRequestmethod. 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 anonDoneEnumeratingcallback onto the returnedEnumeratorresult. 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.onClientErrortakes astatusCodein 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.onClientErrortakes astatusCodein argument, hence your implementation should boil down to:
if(statusCode == play.api.http.Status.BAD_REQUEST) {
// move your implementation of `GlobalSettings.onBadRequest` here
}
-
GlobalSettings.configureandGlobalSettings.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.beforeStartandGlobalSettings.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 initialization when the dependency injection framework loads it. If you need eager initialization (for example, because you need to execute some code before the application is actually started), define an eager binding. -
GlobalSettings.onStop: Add a dependency toApplicationLifecycleon the class that needs to register a stop hook. Then, move the implementation of yourGlobalSettings.onStopmethod inside thePromisepassed to theApplicationLifecycle.addStopHook. Read Stopping/cleaning-up for more information. -
GlobalSettings.onError: Create a class that inherits fromHttpErrorHandler, and move the implementation of yourGlobalSettings.onErrorinside theHttpErrorHandler.onServerErrormethod. Read Error Handling for more information. -
GlobalSettings.onRequest: Create a class that inherits fromDefaultHttpRequestHandler, and move the implementation of yourGlobalSettings.onRequestmethod inside theDefaultHttpRequestHandler.createActionmethod. 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.onClientErrortakes astatusCodein 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.onClientErrortakes astatusCodein 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.