How to extend the playframework?

2019-01-13 05:15发布

问题:

Sometime it's necessary to extends a framework. Sometimes it's necessary to hook into the request/response lifecycle, for example for a parameter binding or to write a security module. How could this be done in the playframework 1.x?

回答1:

There are two ways to extend play 1.x. First you can write your own module. This is described in detail here. This is useful if you want provide a library such as iText or provide a special authentication mechanism.

The second way is to write a PlayPlugin. This is often done in modules but it's not a necessary condition. To write a PlayPlugin requires two steps:

  1. Write a class which extends the class PlayPlugin and override some of its methods, for example called myPackage.MyPlugin.
  2. Register your plugin. This is done by creating a file named play.plugins and putting it into the classpath. The file must contain a line like 1003:myPackage.MyPlugin.

The number defines the order in which the plugins are called. I recommend to use ids > 1000. If you want it to load before a framework plugin, look here (The ids are valid since 1.1.1).

That's it. To get a feeling of what you can do with a Plugin see the javadoc. You can hook into:

  • The request/response-cycle
  • The binding process
  • application start/stop
  • classloading

Unfortunately the javadoc documentation is minimal, but don't hesitate to look into the code of the playframework itself. It's easy to understand and gives you good ideas.