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


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

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

    1.IBaseService

    /**
     * 发送数据接口
     */
    public interface IBaseService {
        void send();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.KafkaServiceImpl

    @Service
    public class KafkaServiceImpl implements IBaseService {
        @Autowired
        MyConfiguration myConfiguration;
    
        @Override
        public void send() {
            System.out.println("调用Kafka接口发送数据!");
            myConfiguration.init("-------------Kafka-------------");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3.MQServiceImpl

    @Service
    public class MQServiceImpl implements IBaseService {
        @Autowired
        MyConfiguration myConfiguration;
    
        @Override
        public void send() {
            System.out.println("调用MQ接口,发送数据!");
            myConfiguration.init("-------------MQ-----------");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4.SendMessageUtil

    /**
     * 根据不同配置文件,选择发送消息工具类
     */
    @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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

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

    application.yaml

    message:
      type: kafka
    
    • 1
    • 2
    	@Autowired
    	SendMessageUtil sendMessageUtil;
    	@Test
    	void contextLoadsTwo() {
    		IBaseService tempBaseService = sendMessageUtil.get();
    		tempBaseService.send();
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    请添加图片描述

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

    1.KafkaCondition

    /**
     * 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

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

    3.MQCondition

    /**
     * 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

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

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

    application.yaml

    message:
      type: kafka
    
    • 1
    • 2
    // 注意:运行contextLoadsTwo测试方法时候,需要将iBaseService全部注释掉,否则会报错
    	// 也要将KafkaServiceImpl和MQServiceImpl上面@Conditional注释掉
    	@Autowired
    	SendMessageUtil sendMessageUtil;
    	@Test
    	void contextLoadsTwo() {
    		IBaseService tempBaseService = sendMessageUtil.get();
    		tempBaseService.send();
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    请添加图片描述

    6.项目结构及源码

    源码奉上,欢迎star!
    MyIdea
    在这里插入图片描述

  • 相关阅读:
    Seal库官方示例(二):encoders.cpp解析
    AWS云计算平台:全方位服务与实践案例
    在android中使用java反射机制的利弊分别是那些?与导入包名类名,androidmk追加对应jar包相比,二者差异是什么?
    Java 如何检测Map集合中是否包含指定value呢?
    Servlet 和 JSP 中的分页
    厨卫电器行业S2B2C系统网站解决方案:打造S2B2C平台全渠道商业系统
    核心函数--少儿编程
    谷粒商城九商品服务商品基本概念与属性分组todo
    隆云通土壤二氧化碳传感器
    Python winreg将cmd/PowerShell(管理员)添加到右键菜单
  • 原文地址:https://blog.csdn.net/jike11231/article/details/133363392