当前位置: 首页 > news >正文

Spring-Smart-DI

参考文章

作用

用注解的方式动态切换实现类实现方式。
比如我们有多个消息中间件或多个短信服务商,需要动态切换的时候,无需自己写判断逻辑来进行服务商的切换。只用一套注解就可以解决问题

开始使用

引入依赖

<dependency><groupId>io.github.burukeyou</groupId><artifactId>spring-smart-di-all</artifactId><version>0.2.0</version>
</dependency>
下载不下来需要拿到jar包打入本地仓库
mvn install:install-file -Dfile=D://spring-smart-di-all-0.2.0.jar -DgroupId=io.github.burukeyou -DartifactId=spring-smart-di-all -Dversion=0.2.0 -Dpackaging=jar

jar包在我的下载里面有上传

开启注解使用

package com.dino;import com.burukeyou.smartdi.annotations.EnableSmartDI;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;@Slf4j
@EnableSmartDI
@SpringBootApplication
public class DinoOaApp {public static void main(String[] args) {log.info("====== Business Starting... ======");ConfigurableApplicationContext ctx = null;try {ctx = new SpringApplicationBuilder(DinoOaApp.class).web(WebApplicationType.SERVLET).run(args);log.info("====== Business Started! ======");} catch (Exception e) {if (ctx != null) {log.error("Business application start failed and exit: {}", e.getMessage(), e);System.exit(SpringApplication.exit(ctx));}}}
}

实现类实现

定义实现类
package com.dino.business.sms;import com.burukeyou.smartdi.proxyspi.spi.EnvironmentProxySPI;
import com.dino.utils.sms.SmsSendRequest;@EnvironmentProxySPI("${sms.impl}")
public interface SmsService {Boolean sendSms(String mobile);
}
实现实现类
package com.dino.business.sms.impl;import com.burukeyou.smartdi.annotations.BeanAliasName;
import com.dino.business.sms.SmsService;
import com.dino.utils.sms.SmsSendRequest;
import com.dino.utils.sms.SmsUtils;
import org.springframework.stereotype.Component;import javax.annotation.Resource;/*** 阿里短信发送*/
@BeanAliasName("ali")
@Component
public class AliSmsServiceImpl implements SmsService {@Resourceprivate SmsUtils smsUtils;@Overridepublic Boolean sendSms(String mobile) {return smsUtils.sendAliSMS(mobile);}
}
package com.dino.business.sms.impl;import com.burukeyou.smartdi.annotations.BeanAliasName;
import com.dino.business.sms.SmsService;
import com.dino.utils.sms.SmsSendRequest;
import com.dino.utils.sms.SmsUtils;
import org.springframework.stereotype.Component;import javax.annotation.Resource;/*** 创蓝短信发送*/
@BeanAliasName("cl")
@Component
public class ClSmsServiceImpl implements SmsService {@Resourceprivate SmsUtils smsUtils;@Overridepublic Boolean sendSms(String mobile) {return smsUtils.sendClSms(mobile);}
}

配置文件配置

# 短信类型  阿里:ali  创蓝 cl
sms:impl: cl
接口调用@AutowiredProxySPIprivate SmsService smsService;public Boolean sendSmsCode(String mobile) {     return smsService.sendSms(mobile);}
http://www.lryc.cn/news/460660.html

相关文章:

  • Python 在树莓派上实现WIFI连接
  • vmware下ubuntu18.04中使用笔记本的摄像头
  • 【Linux】最基本的杂项设备驱动
  • RadioGroup RadioButton底部导航栏
  • SQL分类中的DQL
  • 1.3.ReactOS系统 PAGED_CODE 宏函数的实现
  • 【论文速看】DL最新进展20241015-目标检测、图像超分
  • 京准电钟:NTP时间服务器让大数据时钟同步
  • SSL免费证书申请(七牛云/阿里云等)
  • 十二、结构型(代理模式)
  • 使用 python 下载 bilibili 视频
  • DevExpress WinForms中文教程:Data Grid - 如何点击获取信息?
  • 真空牛肉滚揉机的优点:
  • 【物流配送中心选址问题】基于退火算法混合粒子群算法
  • elasticsearch 8.2 版本账号密码设置及SSL设置
  • git gui基本使用
  • 从automaxprocs库浅窥Linux容器的资源控制
  • AI 读文献(二):综述论文10倍速读和整理
  • 【AAOS】Android Automotive 10模拟器源码下载及编译
  • 前端开发攻略---使用css实现滚动吸附效果
  • 解析 MySQL 查询优化:提升性能的十个关键策略
  • QT--QPushButton设置文本和图标、使能禁能、信号演示
  • PostgreSQL学习笔记六:模式SCHEMA
  • 基础IO -- 理解文件(1)
  • golang包管理
  • outlook 添加企业邮箱账号出现 553 authentication is required 错误解决
  • 一个开源可本地部署的英文翻译服务----EnToZhAPI
  • 【unity】编辑器扩展——在OnValidate中创建、删除游戏物体
  • 学习记录:js算法(六十四):最后一块石头的重量
  • 单片机探秘:从理论到应用