1

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

1:java.util.ServiceLoader

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

向其他实现者提供抽象类

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 等它的核心级别是一样的风格还是他们有一些更高级的方式? 有没有人对此有好主意。

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

4

0 回答 0