Inherit the class play. Playplugin, we can implement plug-in functionality

Source: Internet
Author: User

play1.2.4 version:

Inherit the class play. Playplugin, we can implement plug-in functionality.

The play framework itself is based on this class to provide some plugin that have been implemented, such as: Configurableplugindisablingplugin,coreplugin, Dbplugin, evolutions, Jobsplugin, Jpaplugin, Messagesplugin, Tempfileplugin,validationplugin, WS

This is where we are very familiar with Jobsplugin, implementing some of the timing job,onapplicationstart () and Onapplicationstop () to implement some of our own business when the service starts to stop.

Onapplicationstart () and Onapplicationstop () are methods of the Playplugin class, and of course there are many other ways that you just need to inherit the class and override these methods to implement the plug-in you need.

Open the Play-1.2.4.jar package, in the root directory, we can see the file play.plugins, which lists all the play has implemented plug-ins, the file listed in the plug-in class is loaded on play launch. Open and look at the contents:

0:play. Coreplugin

100:play.data.parsing.tempfileplugin

200:play.data.validation.validationplugin

300:play.db.dbplugin

400:play.db.jpa.jpaplugin

450:play.db.evolutions

500:play.i18n.messagesplugin

600:play.libs.ws

700:play.jobs.jobsplugin

100000:play.plugins.configurableplugindisablingplugin

One row per plug-in class, and the number for the first column is numbered.

So, we want to implement our own plug-in and apply, must also add a new Play.plugins file (the name must be Play.plugins, in the play Jar to write dead, otherwise unable to load), stored in the app directory.

OK, the main content is finished, the following is a simple example, the main implementation of all action calls before doing something, you can do user verification, here is simply a public variable configuration, it is assumed that all templates require this variable:

The first step: Define a class, the name doesn't matter, just inherit the class play. Playplugin, and override method Beforeactioninvocation:

Package plugin;

import Java.lang.reflect.Method;

Import play. Playplugin;

import Play.mvc.Scope;

Publicclass Renderpublicvariableplugin extends playplugin {

@Override

publicvoid beforeactioninvocation (Method actionmethod) {

Scope.renderargs args = Scope.RenderArgs.current ();

Args.put ("Test", "from Plugin:this is the test parameter ' s value!");

}

}

The second step: Add the file Play.plugins in the app directory, the content is as follows:

2000:plugin. Renderpublicvariableplugin

The third step. No, it's that simple, so you can refer to the variable test directly in all the templates.

Follow-up test:

If more than one plug-in implements Beforeactioninvocation, what the program will do, according to the order listed in Play.plugins.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.