2 Star 1 Fork 3

alexgaoyh / pap-spring-boot-spi-demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

#20190621

Saas 平台在设计的过程中,会出现不同租户的业务需求不一致的情况,在这种情况下,则可以考虑使用 SPI 来支持定制化的服务

本例定义基础的 ISPIInterface 接口类,所有牵扯到 SPI 扩展的地方,业务接口(IItemPrice)都继承此接口类

本例定义基础的 SPIInterfaceFactoryBean 类,在具体的调用执行时,根据传入的第一个参数作为条件进行匹配,执行后返回对应的结果;

增加Bean 定义,将 SPIInterfaceFactoryBean 纳入 Spring Bean 容器中;
        /**
         * 定义 FactoryBean
         * @param applicationContext
         * @return
         */
        @Bean
        public SPIInterfaceFactoryBean spiInterfaceFactoryBean(ApplicationContext applicationContext) {
            return new SPIInterfaceFactoryBean(applicationContext, IItemPrice.class);
        }
    
        /**
         *  代理类
         * @param spiInterfaceFactoryBean
         * @return
         * @throws Exception
         */
        @Bean
        public IItemPrice itemPriceProxy(SPIInterfaceFactoryBean spiInterfaceFactoryBean) throws Exception {
            return (IItemPrice) spiInterfaceFactoryBean.getObject();
        }
        
具体的业务使用,详见 Demo 包下的类文件。
    定义不同商品的价格实现类,根据传入的参数是 apple 还是 banana, 返回不同的参数;
    
在 Saas 平台设计的过程中,如果遇到定制化的业务需求的话,则可以实现不同的接口,根据不同的业务场景进行不同的判断。
    1、可以在方法实现上增加注解,走不通的判断;
    2、可以在 conditionVerify 方法上进行判断,满足不同的条件进行调整, 当前示例是判断入参 LoginedUserVO 的参数,根据不同的租户进行不同的业务逻辑处理;
MIT License Copyright (c) 2019 alexgaoyh Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Spring Boot SPI 的示例代码。解决类似 Saas 平台不同租户的业务需求不一致的情况,在这种情况下,则可以考虑使用 SPI 来支持定制化的服务。 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/alexgaoyh/pap-spring-boot-spi-demo.git
git@gitee.com:alexgaoyh/pap-spring-boot-spi-demo.git
alexgaoyh
pap-spring-boot-spi-demo
pap-spring-boot-spi-demo
master

搜索帮助