基于插件的java开发核心内部架构

时间:2013-12-20 08:16:47

标签: java plugins plugin-architecture

我找到了以下用于开发插件的样式

1:java.util.ServiceLoader

工作方式:在META-INF /服务中查找并获取注入服务

将Abstract类提供给其他实现者

2:java.net.URLClassLoader

工作方式:从jar文件和getinstance中加载所需的类。

为其他实施者提供界面

3:从属性文件加载

InputStream in =this.getClass().getClassLoader()
                .getResourceAsStream("packageName/config.properties"); 

工作方式3: 从属性文件加载类名,该文件实现提供的接口

为其他实施者提供界面

3

的示例代码
public interface PluginInterface {
    void performYourOperation();
};


public class PluginImpl implements PluginInterface {

    void performYourOperation() {
        //perform all operation what plugin is inteneded to do 

    }
    Main Loader:

     InputStream in =this.getClass().getClassLoader()
                .getResourceAsStream("packageName/config.properties");
    configProp.load(in );
            String factoryClass = (String) configProp
                    .get("plugin.factory.class");
            Class<?> factory = Class.forName(factoryClass);
            PluginInterface factory=(PluginInterface) factory.newInstance();
            factoryq.performYourOperation();

};

我找到了3种自己的插件实现方式。

除了上面提到的方法之外还有其他方法。

maven和jenkins等核心级别是否具有相同的风格  他们有更先进的方法吗? 任何人都有好主意。

请不要建议使用这个框架,那, 我想学习和理解核心的内部工作方式。谢谢你

0 个答案:

没有答案