巧用@Conditional注解根据配置文件注入不同的bean对象

项目中使用了mq,kafka两种消息队列进行发送数据,为了避免硬编码,在项目中通过不同的配置文件自动识别具体消息队列策略。这里整理两种实施方案,仅供参考!

方案一:创建一个工具类,然后根据配置文件来返回具体的IBase实现类

1.IBaseService

java 复制代码
/**
 * 发送数据接口
 */
public interface IBaseService {
    void send();
}

2.KafkaServiceImpl

java 复制代码
@Service
public class KafkaServiceImpl implements IBaseService {
    @Autowired
    MyConfiguration myConfiguration;

    @Override
    public void send() {
        System.out.println("调用Kafka接口发送数据!");
        myConfiguration.init("-------------Kafka-------------");
    }
}

3.MQServiceImpl

java 复制代码
@Service
public class MQServiceImpl implements IBaseService {
    @Autowired
    MyConfiguration myConfiguration;

    @Override
    public void send() {
        System.out.println("调用MQ接口,发送数据!");
        myConfiguration.init("-------------MQ-----------");
    }
}

4.SendMessageUtil

java 复制代码
/**
 * 根据不同配置文件,选择发送消息工具类
 */
@Component
public class SendMessageUtil {
    //message.type在application.yaml,":kafka" 设置默认值为kafka
    @Value("${message.type:kafka}")
    private String type;

    @Autowired
    KafkaServiceImpl kafkaService;
    @Autowired
    MQServiceImpl mqService;

    public IBaseService get(){
        if (type.equals("kafka"))
            return kafkaService;
        else
            return mqService;
    }
}

5.方案一单元测试及测试结果

application.yaml

yaml 复制代码
message:
  type: kafka
java 复制代码
	@Autowired
	SendMessageUtil sendMessageUtil;
	@Test
	void contextLoadsTwo() {
		IBaseService tempBaseService = sendMessageUtil.get();
		tempBaseService.send();
	}

方案二:@Conditional注解根据配置文件注入不同的bean对象

1.KafkaCondition

java 复制代码
/**
 * Kafka推送方式
 */
public class KafkaCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment environment = context.getEnvironment();
        String type = environment.getProperty("message.type");
        if (type.contains("kafka")){
            return true;
        }
        return false;
    }
}

2.KafkaServiceImpl上面加上@Conditional(KafkaCondition.class)

3.MQCondition

java 复制代码
/**
 * MQ推送方式
 */
public class MQCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment environment = context.getEnvironment();
        String type = environment.getProperty("message.type");
        if (type.contains("mq")){
            return true;
        }
        return false;
    }
}

4.MQServiceImpl上面加上@Conditional(MQCondition.class)

5.方案二单元测试及测试结果

application.yaml

yaml 复制代码
message:
  type: kafka
java 复制代码
// 注意:运行contextLoadsTwo测试方法时候,需要将iBaseService全部注释掉,否则会报错
	// 也要将KafkaServiceImpl和MQServiceImpl上面@Conditional注释掉
	@Autowired
	SendMessageUtil sendMessageUtil;
	@Test
	void contextLoadsTwo() {
		IBaseService tempBaseService = sendMessageUtil.get();
		tempBaseService.send();
	}

6.项目结构及源码

源码奉上,欢迎star!
MyIdea

相关推荐
一 乐4 小时前
网红酒店|基于java+vue的网红酒店预定系统(源码+数据库+文档)
java·开发语言·数据库·毕业设计·论文·springboot·网红酒店预定系统
天上掉下来个程小白16 小时前
Redis-04.Redis常用命令-字符串常用命令
java·数据库·redis·springboot·苍穹外卖
西岭千秋雪_1 天前
Spring Boot自动配置原理解析
java·spring boot·后端·spring·springboot
天上掉下来个程小白1 天前
Redis-06.Redis常用命令-列表操作命令
java·redis·后端·springboot·苍穹外卖
shangxianjiao2 天前
Javaweb后端登录认证 登录校验 过滤器 filter令牌校验,执行流程,拦截路径
java·springboot·springcloud·过滤器
奔跑吧邓邓子2 天前
【商城实战(92)】高并发下的商城缓存进阶:从原理到实战
redis·缓存·springboot·uniapp·element plus·商城实战
奔跑吧邓邓子3 天前
【商城实战(74)】数据采集与整理,夯实电商运营基石
springboot·uniapp·element plus·商城实战·商城数据采集与整理
yimeixiaolangzai3 天前
Spring Boot旅游管理系统
java·mysql·源码·springboot·课程设计
码农爱java4 天前
Elasticsearch 之 ElasticsearchRestTemplate 普通查询
大数据·elasticsearch·全文检索·jenkins·springboot·es·es 查询
老华带你飞6 天前
医院挂号预约小程序|基于微信小程序的医院挂号预约系统设计与实现(源码+数据库+文档)
java·数据库·微信小程序·小程序·毕业设计·springboot·医院挂号预约小程序