codecamp

Hasor 扩展ApiBinder

在 Hasor3 之后,由于 ApiBinder 扩展机制的引入,ApiBinder 变可以随时进行扩展。下面我们展现一下相关特性。例:扩展 Apibinder 并在自己的 TestBinder 上实现一个 Hello Word 方法。

package net.test.binder;
public interface TestBinder extends ApiBinder {
    public void hello();
}
public class TestBinderImpl extends ApiBinderWrap implements TestBinder {
    public TestBinderImpl(ApiBinder apiBinder) {
        super(apiBinder);
    }
    public void hello() {
        System.out.println("Hello Binder");
    }
}
public class TestBinderCreater implements ApiBinderCreater {
    public TestBinder createBinder(ApiBinder apiBinder) {
        return new TestBinderImpl(apiBinder);
    }
}


接着我们需要在配置文件中增加如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://project.hasor.net/hasor/schema/main">
    <hasor.apiBinderSet>
        <binder type="net.test.binder.TestBinder">net.test.binder.TestBinderCreater</binder>
    </hasor.apiBinderSet>
</config>


最后我们在 Hasor 的启动Module中使用这个自定义的 ApiBinder

package net.test.hasor;
public class HelloModule implements Module {
    public void loadModule(ApiBinder apiBinder) throws Throwable {
        TestBinder myBinder = (TestBinder)apiBinder;
        myBinder.hello();
    }
}


Hasor 类扫描
Hasor ApiBinder转换
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }