Hasor 擴(kuò)展ApiBinder

2018-10-05 10:55 更新

在 Hasor3 之后,由于 ApiBinder 擴(kuò)展機(jī)制的引入,ApiBinder 變可以隨時(shí)進(jìn)行擴(kuò)展。下面我們展現(xiàn)一下相關(guān)特性。例:擴(kuò)展 Apibinder 并在自己的 TestBinder 上實(shí)現(xiàn)一個(gè) 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>


最后我們?cè)?Hasor 的啟動(dòng)Module中使用這個(gè)自定義的 ApiBinder

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


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)