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

1、Solon 的三大核心组件

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

2、Solon Plugin 插件扩展机制

几种 Java 扩展机制:

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

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

Plugin 的接口定义:

java 复制代码
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 插件示例

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

java 复制代码
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);
    }
}

插件应用示意:

java 复制代码
@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() {
        ...
    }
}
相关推荐
是乐谷5 分钟前
阿里云杭州 AI 产品法务岗位信息分享(2025 年 8 月)
java·人工智能·阿里云·面试·职场和发展·机器人·云计算
Java水解23 分钟前
Java中的四种引用类型详解:强引用、软引用、弱引用和虚引用
java·后端
lifallen31 分钟前
JCTools 无锁并发队列基础:ConcurrentCircularArrayQueue
java·开发语言·数据结构·算法
一叶飘零_sweeeet39 分钟前
IDEA 插件 Trae AI 全攻略
java·人工智能·intellij-idea
欧哈东哥1 小时前
【C++】标准库中用于组合多个值的数据结构pair、tuple、array...
java·数据结构·c++
python_1361 小时前
web请求和响应
java·spring·github
ciku3 小时前
Spring AI Starter和文档解读
java·人工智能·spring
程序猿阿越3 小时前
Kafka源码(三)发送消息-客户端
java·后端·源码阅读