Compared to the Beijing-racing site source code built in other core components, jfinal to plugin processing is quite concise. This is reflected in the definition of the core interface IPlugin of the plugin component.
- Defined
/**
IPlugin
*/
Public interface IPlugin {
Boolean start ();
Boolean stop ();
}
1
2
3
4
5
6
7
For plugin,jfinal to give enough status--top packagecom.jfinal.plugin.
One thing to note is that in the plugin mechanism, jfinal is only responsible for the start and stop of the plugin, the concrete implementation logic, and the plugin application is completely left to the outside world to decide.
In addition, observing the Com.jfinal.plugin package structure can also detect the jfinal of plugin and the way of design. And from the package structure can also be seen, the general project is basically a few needs. (where Auth has not yet been completed, the idea should be to build a custom session).
Package structure
- Inheritance Chain
After reading the definition, next we look at the plugin inheritance chain system according to the previous habit.
Inheritance Chain
3.1 Activerecordplugin
As one of the core components of the Jfinal ActiveRecord is the start of this plugin, the importance of self-evident.
public boolean start () {
if (isstarted) {
return true;
}
Configure the data source
if (Config.datasource = = NULL && Datasourceprovider! = null) {
Config.datasource = Datasourceprovider.getdatasource ();
}
if (Config.datasource = = null) {
throw new RuntimeException ("ActiveRecord start error:activerecordplugin need DataSource or datasourceprovider");
}
Parsing the SQL Management module joined by Jfinal 3.0
Config.sqlKit.parseSqlTemplate ();
Build the mapping of model between class and table
New Tablebuilder (). Build (tablelist, config);
Dbkit.addconfig (config);
IsStarted = true;
Returns true to inform Jfinal that the plugin started successfully
return true;
}
1234567891011121314151617181920
3.2 C3p0plugin/druidplugin/hikaricpplugin
These three plugin are database connection pooling tools. The core logic is the construction of the DataSource instance.
3.3 Ehcacheplugin/redisplugin
Cache implementations.
3.3 Cron4jplugin
Timed task implementation. The official documents on this category have detailed instructions for use, and users are not using Baidu.
- Life cycle
Finally, let's see how plugin fits into the life cycle of jfinal.
4.1 Start
Jfinalfilter.init
> Jfinal.init
> config.configjfinal (jfinalconfig)
> Config.configpluginwithorder (1, jfinalconfig);
> Config.startplugins (); The jfinal will start plugin before a different jfinalconfig.configxxxx, depending on the user configuration (Constants.setconfigpluginorder ()). 】
12345
4.2 Stop
Jfinalfilter.destroy
> Jfinal.stopplugins ()
12
- Summarize
Jfinal at the framework level for plugin processing, only responsible for callback plugin start and stop, specific implementation logic, as well as the plugin application is completely left to the outside world to complete.
Very few mandatory contracts, giving plugin a very strong flexibility to meet custom requirements on a variety of projects.
Jfinal Beijing-Racing website source code Building Research core Components plugin