巧用@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

相关推荐
LUCIAZZZ1 天前
SkyWalking快速入门
java·后端·spring·spring cloud·微服务·springboot·skywalking
ccmjga3 天前
升级 SpringBoot3 全项目讲解 — Spring Boot 3 中如何发Http请求?
java·spring boot·后端·docker·gradle·springboot·spring security
敲代码的小王!5 天前
在Java项目中跨域的解决办法
java·开发语言·微服务·springboot
蔚一8 天前
微服务SpringCloudAlibaba组件Spring Cloud Gateway网关教程【详解gatway网关以及各种过滤器配置使用,附有示例+代码】
java·spring boot·后端·微服务·gateway·springboot·intellij idea
秋意钟9 天前
你认为如何理解“约定大于配置”?
springboot
清风絮柳9 天前
41.兼职网站管理系统(基于springboot&&vue的Java项目)
java·开发语言·vue.js·毕业设计·springboot·兼职网站管理系统
松树戈9 天前
mybatis-plus逆向code generator pgsql实践
mybatis·springboot
阿Q说代码9 天前
SpringBoot实战:高效获取视频资源
实战·springboot·短视频
蔚一10 天前
微服务SpringCloud Alibaba组件nacos教程【详解naocs基础使用、服务中心配置、集群配置,附有案例+示例代码】
java·后端·spring cloud·微服务·架构·intellij-idea·springboot
2301_7930698211 天前
前后端分离的网页游戏,后端spring boot,前端vite+vue
前端·后端·vue·springboot·全栈