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 {
    @Resource
    private SmsUtils smsUtils;
    @Override
    public 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 {
    @Resource
    private SmsUtils smsUtils;
    @Override
    public Boolean sendSms(String mobile) {
        return smsUtils.sendClSms(mobile);
    }
}

配置文件配置

# 短信类型  阿里:ali  创蓝 cl
sms:
  impl: cl
接口调用
    @AutowiredProxySPI
    private SmsService smsService;

    public Boolean sendSmsCode(String mobile) {     
        return smsService.sendSms(mobile);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值