Spring DI (Beans) with multiple concretes…picking one of them

后端 未结 2 733
小鲜肉
小鲜肉 2020-12-12 06:24

I have a similar question here

Guice with multiple concretes......picking one of them

with a solution for Guice.

But I have a different project usin

2条回答
  •  鱼传尺愫
    2020-12-12 06:51

    I think I have something that works:

    beans.xml (note the "util" extras in the namespace declares)

    
    
    
        
            
        
    
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
            
            
        
    
        
            
            
        
    
    
    
    

    and java

     package com.me.shipping;
    
    
    import java.util.Collection;
    import java.util.Map;
    import java.util.Set;
    
    import org.apache.commons.logging.Log;
    
    import com.me.shipping.interfaces.OrderProcessorInterface;
    import com.me.shipping.interfaces.ShipperInterface;
    import com.me.Models.Order;
    
    
    public class OrderProcessorImpl implements OrderProcessorInterface {
    
      private Log logger;
      private java.util.Map shipperInterfaceMap;
    
    
      public OrderProcessorImpl(Log lgr, java.util.Map siMap) {
    
        if (null == lgr) {
          throw new IllegalArgumentException("Log is null");
        }
    
        if (null == siMap) {
          throw new IllegalArgumentException("Map is null");
        }
    
        this.logger = lgr;
        this.shipperInterfaceMap = siMap;
      }
    
      public void ProcessOrder(String preferredShipperAbbreviation, Order ord) {
        this.logger.info(String.format("About to ship. (%1s)", preferredShipperAbbreviation));
    
        ShipperInterface foundShipperInterface = this.FindShipperInterface(preferredShipperAbbreviation);
        foundShipperInterface.ShipOrder(ord);
      }
    
        private ShipperInterface FindShipperInterface(String friendlyName)
        {
            ShipperInterface returnItem = null;
            if (null != this.shipperInterfaceMap)
            {
                returnItem = this.shipperInterfaceMap.entrySet().stream()
                        .filter(e -> e.getKey().equalsIgnoreCase(friendlyName))
                          .map(Map.Entry::getValue)
                          .findFirst()
                          .orElse(null);
            }
    
            if (null == returnItem)
            {
                throw new NullPointerException(String.format("shipperProviderMap did not contain expected item. (Key='%s')", friendlyName));
            }
    
            return returnItem;
        }
    }
    

    and "main" method

            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            BeanFactory factory = context;
    
            Order ord = new Order();
            OrderProcessorInterface opi = context.getBean(OrderProcessorImpl.class);
            opi.ProcessOrder("fedexFriendlyName", ord);
    

提交回复
热议问题