All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.starblues.realize.PluginUtils Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package com.gitee.starblues.realize;

import com.gitee.starblues.utils.SpringBeanUtils;
import org.pf4j.PluginDescriptor;
import org.springframework.context.ApplicationContext;

import java.util.List;

/**
 * 插件工具类
 * @author starBlues
 * @version 2.4.0
 */
public class PluginUtils {

    protected final ApplicationContext parentApplicationContext;
    protected final ApplicationContext pluginApplicationContext;
    protected final PluginDescriptor pluginDescriptor;

    public PluginUtils(ApplicationContext parentApplicationContext,
                       ApplicationContext pluginApplicationContext,
                       PluginDescriptor pluginDescriptor) {
        this.parentApplicationContext = parentApplicationContext;
        this.pluginApplicationContext = pluginApplicationContext;
        this.pluginDescriptor = pluginDescriptor;
    }

    /**
     * 获取主程序的 ApplicationContext
     * @return ApplicationContext
     */
    public ApplicationContext getMainApplicationContext() {
        return parentApplicationContext;
    }

    /**
     * 获取当前插件的 ApplicationContext
     * @return ApplicationContext
     */
    public ApplicationContext getPluginApplicationContext() {
        return pluginApplicationContext;
    }

    /**
     * 获取当前插件的描述信息
     * @return PluginDescriptor
     */
    public PluginDescriptor getPluginDescriptor(){
        return pluginDescriptor;
    }


    /**
     * 通过 bean名称得到主程序中的bean
     * @param name bean 名称
     * @param  bean 类型
     * @return bean
     */
    public  T getMainBean(String name){
        Object bean = parentApplicationContext.getBean(name);
        if(bean == null){
            return null;
        }
        return (T) bean;
    }

    /**
     * 通过bean类型得到主程序中的bean
     * @param aClass bean 类型
     * @param  bean 类型
     * @return bean
     */
    public  T getMainBean(Class aClass) {
        return parentApplicationContext.getBean(aClass);
    }

    /**
     * 通过接口或者抽象类类型得到主程序中的多个实现对象
     * @param aClass bean 类型
     * @param  bean 类型
     * @return bean
     */
    public  List getMainBeans(Class aClass){
        return SpringBeanUtils.getBeans(parentApplicationContext, aClass);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy