[Java] Solon 框架的三大核心组件之一插件扩展体系

177 阅读2分钟

1、Solon 的三大核心组件

核心组件说明
Plugin 插件扩展机制提供“编码风格”的扩展体系
Ioc/Aop 应用容器提供基于注入依赖的自动装配体系
Context+Handler 通用上下文处理接口提供“开放式处理”适配体系(俗称,三元合一)

2、Solon Plugin 插件扩展机制

几种 Java 扩展机制:

扩展机制描述特点体验风格适用性
Java SPIJava 自带的以接口为单位配置风格适用于所有 Java 生态(最通用)
Spring FactoriesSpring 框架提供的以组件为单位配置风格适用于 Spring 生态体系
Solon PluginSolon 框架提供的以模块为单位编码风格适用于 Solon 生态体系

Solon Plugin 是 Java SPI 的一种“增强”模式,强调编码风格。插件模块元信息配置会申明一个 Plugin 接口的实现类,在应用启动时扫描元信息目录,以发现所有申明的插件实现。

Plugin 的接口定义:

public interface Plugin {
    //启动
    void start(AppContext context) throws Throwable;
    //预停止
    default void prestop() throws Throwable{}
    //停止
    default void stop() throws Throwable{}
}

Plugin 实现类的元信息配置申明:以 META-INF/solon 为专属目录;使用 properties 格式;要配置插件的实现类及优先级。

# META-INF/solon/{packname}.properties

solon.plugin={PluginImpl}   #插件实现类配置
solon.plugin.priority=1 #插件优化级配置。越大越优先,默认为0

3、Solon Plugin 插件示例

用一个数据缓存与事务相关的插件为例,以模块为单位实现整体装配(编码风格):

public class DemoSolonPlugin implements Plugin {
    @Override
    public void start(AppContext context) {
        if (context.app() != null) {
            //添加事务控制支持
            if (context.app().source().isAnnotationPresent(EnableTransaction.class)) {
                //添加注解拦截器
                context.beanInterceptorAdd(Tran.class, TranInterceptor.instance, 120);
            }

            //添加缓存控制支持
            if (context.app().source().isAnnotationPresent(EnableCaching.class)) {
                //添加注解拦截器
                context.beanInterceptorAdd(CachePut.class, new CachePutInterceptor(), 110);
                context.beanInterceptorAdd(CacheRemove.class, new CacheRemoveInterceptor(), 110);
                context.beanInterceptorAdd(Cache.class, new CacheInterceptor(), 111);
            }
        }
        
        //根据配置自动构建数据源
        context.beanMake(DataSourcesAutoConfiguration.class);
    }
}

插件应用示意:

@EnableTransaction
@EnableCaching
public class App {
    public static void main(String[] args) {
        Solon.start(App.class, args);
    }
}

@Component
public class DemoService {
    @Cache
    public String test() {
        return new Date().toString();
    }
    
    @Tran
    public void post() {
        ...
    }
}