Jfinal---JFinalConfig解析

来源:互联网 发布:手机淘宝 平板 编辑:程序博客网 时间:2024/06/15 03:38

1.概述
基于 JFinal 的 web 项目需要创建一个继承自 JFinalConfig 类的子类,该类用于对整个 web
项目进行配置。
JFinalConfig 子类需要实现五个抽象方法,如下所示:

public class DemoConfigextends JFinalConfig{     publicvoid configConstant(Constants me){}     public void configRoute(Routesme) {}    publicvoid configPlugin(Plugins me) {}    public void configInterceptor(Interceptors me) {}    public void configHandler(Handlersme) {}}

2.2 configConstant(Constants me)

此方法用来配置 JFinal 常量值,
如开发模式常量 devMode 的配置,默认视图类型 ViewType的配置

public void configConstant(Constants me) {        //设置开发模式为true        me.setDevMode(true);        //设置默认视图类型为jsp        me.setViewType(ViewType.JSP);        // 加载少量必要配置,随后可用getProperty(...)获取值        loadPropertyFile("a_little_config.txt");        me.setMaxPostSize(2000000000);    }

在开发模式下,JFinal 会对每次请求输出报告,如输出本次请求的 Controller、Method 以 及请求所携带的参数。JFinal 支持 JSP、FreeMarker、Velocity 三种常用视图。

2.3 configRoute(Routes me)

此方法用来配置 JFinal 访问路由,一般为了方便管理,会拆分一下

public void configRoute(Routes me) {        // 前端路由,创建AdRoutes类管理        me.add(new AdRoutes());         //直接添加        me.add("/hello", HelloController.class);    }

Routes 类主要有如下两个方法:

public Routes add(String controllerKey, Class<? extends Controller>controllerClass, String viewPath)public Routes add(String controllerKey, Class<? extends Controller>    controllerClass) 
//路由管理类public class AdRoutes extends Routes{    @Override     public void config() {        //对应路由信息        add("/admin", AdminController.class);        add("/dsp", DspController.class);        add("/multiios", MultiIosController.class);        add("/lucky", LuckyController.class);        add("/onlineGame", OnlineGameController.class);        add("/wifiNews", WifiNewsController.class);    }}

JFinal 路由规则如下表:
JFinal 路由规则

JFinal 在以上路由规则之外还提供了 ActionKey 注解,可以打破原有规则,以下是代码示 例:

public class UserController extends Controller {    @ActionKey("/login")    public void login(){         render("login.html");    }}

假定 UserController 的 controllerKey 值为“/user”,在使用了@ActionKey(“/login”)注解以 后,actionKey 由原来的“/user/login”变为了“/login”。该注解还可以让 actionKey 中使用减号或 数字等字符,如“/user/123-456”。

JFinal 路由还可以进行拆分配置,这对大规模团队开发特别有用,以下是代码示例:

//前端路由public class FrontRoutes extends Routes {    public void config(){        add("/",IndexController.class);        add("/blog", BlogController.class);    }}
//后端路由public class AdminRoutesextends Routes{    public void config(){    add("/admin",AdminController.class);    add("/admin/user", UserController.class);    }}
public class MyJFinalConfigextends JFinalConfig{    publicvoid configRoute(Routesme) {         me.add(new FrontRoutes());  // 前端路由         me.add(new AdminRoutes()); // 后端路由    }    public void configConstant(Constantsme) {}    public void configPlugin(Pluginsme) {}    public void configInterceptor(Interceptorsme) {}    public void configHandler(Handlersme) {}}

如上三段代码,FrontRoutes 类中配置了系统前端路由,AdminRoutes 配置了系统后端路由, MyJFinalConfig.configRoute(…)方法将拆分后的这两个路由合并起来。使用这种拆分配置不仅 可以让 MyJFinalConfig 文件更简洁, 而且有利于大规模团队开发, 避免多人同时修改 MyJFinalConfig 时的版本冲突。


如果 JFinal 默认路由规则不能满足需求,开发者还可以根据需要使用 Handler 定制更加个 性化的路由,大体思路就是在 Handler 中改变第一个参数 String target 的值。

2.4 configPlugin (Plugins me)

此方法用来配置 JFinal 的 Plugin,如下代码配置了 C3p0 数据库连接池插件与 ActiveRecord数据库访问插件。通过以下的配置,可以在应用中使用 ActiveRecord 非常方便地操作数据库。

public void configPlugin(Pluginsme){     //加载配置文件    loadPropertyFile("your_app_config.txt");    //c3p0连接,可创建多个不同连接,连接不同数据库    C3p0Plugin c3p0Plugin =     new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"));    me.add(c3p0Plugin);    // 配置ActiveRecord插件,不指定configName    ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);    //指定configName    ActiveRecordPlugin arp = new ActiveRecordPlugin("configName",c3p0Plugin);    me.add(arp);    // 方式一: 直接配置数据表映射    //参数(String tableName, String primaryKey, Class<? extends Model<?>> modelClass)                     arp.addMapping("user", "id", User.class);    //(String tableName, Class<? extends Model<?>> modelClass)    arp.addMapping("user",User.class);   // 方式二:配置数据表映射写到一个文件中   // DbMappingKit.mapping(arp);    //redis插件    //(String cacheName, String host, int port, int timeout, String password)    RedisPlugin yunyingRedis =             new RedisPlugin("yunying", "120.26.234.218",7779,5000,"Y1z+yYGhQ1z8xI8f");    me.add(yunyingRedis);    //memcahe插件    //(String host, int port)    MemcachedPlugin newsMemcached = new MemcachedPlugin("127.0.0.1", 11211);    me.add(newsMemcached);

数据库映射的第二种方式:映射表的文件DbMappingKit

public class DbMappingKit {    public static void mapping(ActiveRecordPlugin arp) {        arp.addMapping("user", "id", User.class);    }}

2.5 configInterceptor (Interceptors me)
此方法用来配置 JFinal 的全局拦截器,全局拦截器将拦截所有 action 请求,除非使用@Clear 在 Controller 中清除,如下代码配置了名为 AuthInterceptor 的拦截器。

public void configInterceptor(Interceptorsme){      me.add(new AuthInterceptor());}

JFinal 的 Interceptor 非常类似于 Struts2,但使用起来更方便,Interceptor 配置粒度分为 Global、Class、Method 三个层次,其中以上代码配置粒度为全局。

拦截器有三种配置级别:    1)全局拦截器:在JFinalConfig实现类的public void configInterceptor(Interceptors me)方法里添加拦截器,对所有的Controller有效;    2)Controller拦截器:通过@Before(StudentInterceptor.class)注释在类的最顶部,只对这个Controller有效;    3)Action拦截器:通过@Before(StudentValidator.class)注释在对应的action方法体上,请求该action时会先经过拦截器处理。注意:如果需要通过注解的方式配置多个拦截器可以如下方式:    @Before({StudentValidator.class, StudentValidator.class})
ps:    如果已经配置了拦截器但是又想在某个action中清除掉拦截器,可以通过注解:@ClearInterceptor(ClearLayer.ALL)清除所有的拦截器,如果没写括号参数,默认清除上一级的。1)action清除上一级为controller级;2)controller级别为全局级。

2.6 configHandler (Handlers me)

此方法用来配置 JFinal 的 Handler,如下代码配置了名为 ResourceHandler 的处理器,Handler 可以接管所有 web 请求,并对应用拥有完全的控制权,可以很方便地实现更高层的功能性扩 展。

(具体使用场景未知)

public void configHandler(Handlers me) {     me.add(new ResourceHandler());}

2.7 afterJFinalStart()与 beforeJFinalStop()

JFinalConfig 中的 afterJFinalStart()与 beforeJFinalStop()方法供开发者在 JFinalConfig 继承类中 覆盖 。 JFinal 会在系统启动完成后回调 afterJFinalStart() 方 法 , 会 在 系 统 关 闭 前 回 调 beforeJFinalStop()方法。这两个方法可以很方便地在项目启动后与关闭前让开发者有机会进行 额外操作,如在系统启动后创建调度线程或在系统关闭前写回缓存。

2.8 PropKit

PropKit 工具类用来操作外部配置文件。PropKit 可以极度方便地在系统任意时空使用,如 下是示例代码:

public class AppConfigextends JFinalConfig{    public void configConstant(Constantsme) {        // 第一次使用use加载的配置将成为主配置,可以通过PropKit.get(...)直接取值         PropKit.use("a_little_config.txt");        me.setDevMode(PropKit.getBoolean("devMode"));    }    public void configPlugin(Pluginsme) {        // 非第一次使用use加载的配置,需要通过每次使用use来指定配置文件名再来取值         String redisHost= PropKit.use("redis_config.txt").get("host");         int redisPort= PropKit.use("redis_config.txt").getInt("port");         RedisPlugin rp =new RedisPlugin("myRedis", redisHost, redisPort);        me.add(rp);        // 非第一次使用 use加载的配置,也可以先得到一个Prop对象,再通过该对象来获取值         Prop p =PropKit.use("db_config.txt");        DruidPlugin dp = new DruidPlugin(p.get("jdbcUrl"), p.get("user")…); me.add(dp);    }}

如上代码所示,PropKit 可同时加载多个配置文件,第一个被加载的配置文件可以使用 PorpKit.get(…)方法直接操作,非第一个被加载的配置文件则需要使用 PropKit.use(…).get(…) 来操作。PropKit 的使用并不限于在 YourJFinalConfig 中,可以在项目的任何地方使用, JFinalConfig 的 getProperty 方法其底层依赖于 PropKit 实现。

以上都是自己在学习过程中的总结整理,如果有错误的地方,欢迎留言指正。

原创粉丝点击