• spring bean实例注入到map 集合中


    说明

    1、动态获取bean实例、根据不同的beanName获取不同的bean
    2、根据不同的策略获取不懂的bean实例【策略模式

    背景:

    在项目经常会使用到,不同的角色调用不同的业务方法,实现不同的逻辑,如果使用 if (roleType = 1) { 角色1逻辑 } else if(roleType = 2) { 角色2逻辑} …,这样的代码看起来比较臃肿; 使用策略模式,调用不同的bean实例,不同的业务逻辑,这样代码看起来就舒畅很多

    配置

    1、一个实例工厂,如:FruitsFactory 【使用@Autowired注入bean实例到Map集合】
    2、一个接口,如:Fruits
    3、Fruits对应的实现类,如:AppleImpl、OrangeImpl、…

    demo如下

    1 实例工厂

    @Service
    @RequiredArgsConstructor
    @Slf4j
    public class FruitsFactory {
    
    //    @Autowired
    //    private Map beans = new ConcurrentHashMap<>(3);
        private final Map<String, Fruits> beans;
    
        public Fruits getBean(String beanName) {
            Fruits shuiGuo = beans.get(beanName);
            return shuiGuo;
        }
    
        /**
         * 打印bean相关信息
         * @author kazaf @date 2022/11/15 10:15
         */
        public void printBeans() {
            for (Map.Entry<String, Fruits> temp : beans.entrySet()) {
                String beanName = temp.getKey();
                Fruits fruits = temp.getValue();
                String result = fruits.eat(beanName);
                log.info("beanName={},result>:{}", beanName, result);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    2 接口

    public interface Fruits {
    
    
        String eat(String name);
    //    int insert();
    //    int update();
    //    int select();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3 实现类

    
    @Service("type.orange") // bean名称也可以使用枚举
    public class OrangeImpl implements Fruits {
    
        @Override
        public String eat(String name) {
            return "橘子我要吃"+name;
        }
    }
    
    @Service("type.apple")
    public class AppleImpl implements Fruits {
        @Override
        public String eat(String name) {
            return "苹果我要吃"+name;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    4 调用类,controller或其他类都行

    • eatFruits 方法,传入不同的bean名称,执行不同的逻辑
    • printBeans 方法 打印结果:
      beanName=type.apple,result>:苹果我要吃type.apple
      beanName=type.orange,result>:橘子我要吃type.orange
    
    @Slf4j
    @Validated
    @RestController
    @RequestMapping("test")
    @RequiredArgsConstructor
    public class TestRest {
    
        private final FruitsFactory fruitsFactory; // 水果工厂
    
        @GetMapping("eatFruits")
        public String eatFruits(@RequestParam String type) {
    
            log.info("---eatFruits.param>:type={}", type);
            String result = fruitsFactory.getBean(type).eat("");
            return result;
        }
    
        /**
         * 打印bean
         * @author kazaf @date 2022/11/15 10:00
         * @return java.lang.String
         */
        @GetMapping("printBeans")
        public String printBeans() {
    
            fruitsFactory.printBeans();
            return "success";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
  • 相关阅读:
    [GXYCTF2019]BabyUpload(.htaccess)
    【若依】前后端分离版本部署到Tomcat完整过程
    有关于阶乘的相关理解
    数据结构与算法(C语言版)P5---栈
    AI生成PPT:如何轻松制作专业的答辩PPT?
    青莲地心火 斗破 —— 双向带头循环链表
    界面控件DevExtreme React Grid—如何将数据导出到Excel文档?
    C语言字符串查找函数和错误信息报告函数(strstr、strtok,strerror)
    【OpenCV-Python】教程:3-10 直方图(2)直方图均衡
    企业寄件管理方案教程
  • 原文地址:https://blog.csdn.net/qiaziliping/article/details/127862692