zoukankan      html  css  js  c++  java
  • 工厂模式如何返回Spring的Bean

      工厂返回的可以是一个具体的对象,比如造一辆车,可以返回一个自行车对象,或者汽车对象。
      但是在Spring 中需要工厂返回一个具体的Service,这就是一个抽象工厂了

      一种方法是反射,个人觉得这种方式不好;
      还有一种方法是巧妙的使用Map对象,工厂的一个优点就是可扩展,对于这种方式可以说是体现的淋漓尽致了,可以定义多个map,map里也可以扩充


      假设现在有一个接口类:BingService
      以及实现了这个接口的两个实现类: OneBingServiceImpl,TwoBingServiceImpl

      1、在工厂类里定义Map

    import java.util.Map;
    
    public class BingServiceFactory {
    
        //Map中的Value是 ServiceBean
        private Map<String, BingService> serviceMap;
    
        //返回对应的 Service
        public BingService getBingService(String platform) {
            return serviceMap.get(platform);
        }
    
        public Map<String, BingService> getServiceMap() {
            return serviceMap;
        }
    
        public void setServiceMap(Map<String, BingService> serviceMap) {
            this.serviceMap = serviceMap;
        }
    }

      2、是用注解方式,配置工厂,同时使用set 注入的方法,给用到工厂的bean来set一下

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.annotation.Resource;
    import java.util.HashMap;
    import java.util.Map;
    
    @Configuration
    public class BingConfiguration {
        @Resource
        private OneServiceImpl oneService;
    
        @Resource
        private TwoServiceImpl twoService;
    
        @Resource
        private TestServiceImpl testService;
    
        @Bean
        public BingServiceFactory createFactory() {
            BingServiceFactory factory = new BingServiceFactory();
    
            Map<String, BingService> serviceMap = new HashMap<>();
            serviceMap.put("One",oneService);
            serviceMap.put("Two",twoService);
            factory.setServiceMap(serviceMap);
    
            testService.setFactory(factory);
    
            return factory;
        }
    
    }

    @Bean 注解如果无效的话,可能得 @Bean("xxxxServiceFactory")  这样的

      3、使用set 注入的方方式来获取工厂(当然也可以使用Autowired 注解注入)

    import org.springframework.stereotype.Component;
    
    @Component
    public class TestServiceImpl {
    
        private BingServiceFactory factory;
    
        public void test() {
            BingService service = factory.getBingService("One");
        }
    
        public BingServiceFactory getFactory() {
            return factory;
        }
    
        public void setFactory(BingServiceFactory factory) {
            this.factory = factory;
        }
    }

      这个工厂可以优化的,不要Factory 这个类,直接使用Map 就行

      原创文章,欢迎转载,转载请注明出处!

  • 相关阅读:
    Effective C# 原则37:使用标准的配置机制(译)
    Effective C# 原则31:选择小而简单的函数(译)
    Effective C# 原则24:选择申明式编程而不是命令式编程(译)
    Effective C# 原则34:创建大容量的Web API(译)
    Effective C# 原则27:避免使用ICloneable(译)
    Effective C# 第4章:创建基于二进制的组件(译)
    Effective C# 原则39:使用.Net验证(译)
    Effective C# 原则35:选择重写函数而不是使用事件句柄(译)
    Effective C# 原则25: 让你的类型支持序列化(译)
    Effective C# 原则38:使用和支持数据绑定(译)
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/spring_factory.html
Copyright © 2011-2022 走看看