您现在的位置是:首页 > 文章详情

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

日期:2025-01-21点击:70

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 的接口定义:

 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() { ... } }
原文链接:https://www.oschina.net/news/330738
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章