Module


Create a custom module
  • Step 1: Create a business interface that needs to be exposed to the outside world according to business requirements

    public interface IDemoModule {
    
        // 为方便引用,定义模块名称常量
        String MODULE_NAME = "demomodule";
    
        // 返回自定义模块的参数配置接口对象
        IDemoModuleCfg getModuleCfg();
    
        // 对外暴露的业务方法
        String sayHi();
    }
  • Step 2: Process the configuration parameters of the custom module. The following code assumes that the test module has two custom parameters

    // 定义模块配置接口
    public interface IDemoModuleCfg {
    
        String getModuleParamOne();
    
        String getModuleParamTwo();
    }
    
    // 实现模块配置接口
    public class DemoModuleCfg implements IDemoModuleCfg {
    
        private String __moduleParamOne;
    
        private String __moduleParamTwo;
    
        public DemoModuleCfg(YMP owner) {
            // 从YMP框架中获取模块配置映射
            Map<String, String> _moduleCfgs = owner.getConfig().getModuleConfigs(IDemoModule.MODULE_NAME);
            //
            __moduleParamOne = _moduleCfgs.get("module_param_one");
            __moduleParamTwo = _moduleCfgs.get("module_param_two");
        }
    
        public String getModuleParamOne() {
            return __moduleParamOne;
        }
    
        public String getModuleParamTwo() {
            return __moduleParamTwo;
        }
    }
  • Step 3: Implement the module and business interface

    Note: Be sure not to forget to declare the @Module annotation on the module implementation class, so that Automatically scanned, loaded and initialized by the YMP framework;

    @Module
    public class DemoModule implements IModule, IDemoModule {
    
        private YMP __owner;
    
        private IDemoModuleCfg __moduleCfg;
    
        private boolean __inited;
    
        public String getName() {
            return IDemoModule.MODULE_NAME;
        }
    
        public void init(YMP owner) throws Exception {
            if (!__inited) {
                __owner = owner;
                __moduleCfg = new DemoModuleCfg(owner);
                //
                __inited = true;
            }
        }
    
        public boolean isInited() {
            return __inited;
        }
    
        public YMP getOwner() {
            return __owner;
        }
    
        public IDemoModuleCfg getModuleCfg() {
            return __moduleCfg;
        }
    
        public void destroy() throws Exception {
            if (__inited) {
                __inited = false;
                //
                __moduleCfg = null;
                __owner = null;
            }
        }
    
        public String sayHi() {
            return "Hi, YMP!";
        }
    }
  • Step 4: Add it to the YMP configuration file ymp-conf.properties Module configuration content

    Format: ymp.configs.<Module name>.<Parameter name>=[Parameter value]

    ymp.configs.demomodule.module_param_one=module_param_one_value
    ymp.configs.demomodule.module_param_two=module_param_two_value
Calling custom modules
public static void main(String[] args) throws Exception {
    YMP.get().init();
    try {
        // 获取自定义模块实例对象
        IDemoModule _demoModule = YMP.get().getModule(IDemoModule.class);
        // 调用模块业务接口方法
        System.out.println(_demoModule.sayHi());
        // 调用模块配置信息
        System.out.println(_demoModule.getModuleCfg().getModuleParamOne());
    } finally {
        YMP.get().destroy();
    }
}

Note: Custom modules do not Supports IoC, AOP and other features;