0

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 using spring di (beans), but with the same kind of issue.

I have an interface with N number of concretes. (3 here)

public interface OrderProcessorInterface {

  void ProcessOrder(String preferredShipperAbbreviation, Order ord);

}

public class FedExShipper implements ShipperInterface {

  private Log logger;

  public FedExShipper(Log lgr) {

    if (null == lgr) {
      throw new IllegalArgumentException("Log is null");
    }

    this.logger = lgr;
  }

  public void ShipOrder(Order ord) {
    this.logger.info("I'm shipping the Order with FexEx");
  }
}


public class UpsShipper implements ShipperInterface {

  private Log logger;

  public UpsShipper(Log lgr) {

    if (null == lgr) {
      throw new IllegalArgumentException("Log is null");
    }

    this.logger = lgr;
  }

  public void ShipOrder(Order ord) {
    this.logger.info("I'm shipping the Order with Ups");
  }
}


public class UspsShipper implements ShipperInterface {

  private Log logger;

  public UspsShipper(Log lgr) {

    if (null == lgr) {
      throw new IllegalArgumentException("Log is null");
    }

    this.logger = lgr;
  }

  public void ShipOrder(Order ord) {
    this.logger.info("I'm shipping the Order with Usps");
  }
}

........

Then I have a class that needs to know about ALL THREE concretes.

import java.util.Collection;
import java.util.Set;

import org.apache.commons.logging.Log;

public class OrderProcessorImpl implements OrderProcessorInterface {

  private Log logger;
  private java.util.Map<String, javax.inject.Provider<ShipperInterface>> shipperProviderMap;

  public OrderProcessorImpl(Log lgr, java.util.Map<String, javax.inject.Provider<ShipperInterface>> spMap) {

    if (null == lgr) {
      throw new IllegalArgumentException("Log is null");
    }

    if (null == spMap) {
      throw new IllegalArgumentException("Provider<ShipperInterface> is null");
    }

    this.logger = lgr;
    this.shipperProviderMap = spMap;
  }

  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 preferredShipperAbbreviation) {

    ShipperInterface foundShipperInterface = this.shipperProviderMap.get(preferredShipperAbbreviation).get();

    if (null == foundShipperInterface) {
      throw new NullPointerException(
          String.format("ShipperInterface not found in shipperProviderMap. ('%1s')", preferredShipperAbbreviation));
    }

    return foundShipperInterface;
  }
}

=============

Basically, I want to call the method, pass in a string argument, and have it choose the concrete for me. (if my real code, this is via a database value, but for the demo code, this is good enough)

Order ord = new Order();
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
BeanFactory factory = context;

OrderProcessorInterface opi = context.getBean(OrderProcessorImpl.class);
opi.ProcessOrder("myFedExName", ord); /* friendlyName would be nice, but fully qualified concrete name also assceptable */

My Spring Configuration is via xml:

 <bean id="theLoggerBean"
       class="org.apache.commons.logging.impl.Log4JLogger">
       <constructor-arg value="log" />
 </bean>    



<bean id="fedExBean"
    class="com.me.FedExShipper">
    <constructor-arg ref="theLoggerBean"></constructor-arg>
</bean>


<bean id="uspsExBean"
    class="com.me.FedExShipper">
    <constructor-arg ref="theLoggerBean"></constructor-arg>
</bean>


<bean id="upsExBean"
    class="com.me.FedExShipper">
    <constructor-arg ref="theLoggerBean"></constructor-arg>
</bean>

..........

================================

<bean id="OrderProcessorImplBean"
    class="com.me.OrderProcessorImpl">

    <constructor-arg ref="theLoggerBean"></constructor-arg>

    <constructor-arg ref="How do I do N Number of ShipperInterfaces Here ??"></constructor-arg>

</bean>

So I want to xml configure the 3 concretes.

And then inject them into the class.

But where I have "How do I do N Number of ShipperInterfaces Here ??", I have no idea what to do.

JSR 330 implementation preferred, but will take anything.

THANKS

Note, in the other question (the Guice one), this was also a possiblity for the constructor of the OrderProcessor:

public class OrderProcessorImpl implements OrderProcessorInterface {

  private Log logger;
  Set<ShipperInterface> shippers;

  public OrderProcessorImpl(Log lgr, Set<ShipperInterface> shprs) {

    if (null == lgr) {
      throw new IllegalArgumentException("Log is null");
    }

    if (null == shprs) {
      throw new IllegalArgumentException("ShipperInterface(s) is null");
    }

    this.logger = lgr;
    this.shippers = shprs;
  }

  public void ProcessOrder(String preferredShipperAbbreviation, Order ord) {
    this.logger.info(String.format("About to ship. (%1s)", preferredShipperAbbreviation));

    for (ShipperInterface sh : shippers) {
      this.logger.info(String.format("ShipperInterface . (%1s)", sh.getClass().getSimpleName()));
    }

  }
}
granadaCoder
  • 26,328
  • 10
  • 113
  • 146

2 Answers2

1

Something like this should work. This uses @Autowired and not xml configuration:

@org.springframework.stereotype.Service
public class OrderProcessorImpl implements OrderProcessorInterface {

    private List<ShipperInterface> shipperProviders;

    private Map<String, ShipperInterface> shipperProvidersMap = new HashMap<>();

    @Autowired
    public void setShipperProviders(List<ShipperInterface> shipperProviders) {
        this.shipperProviders= shipperProviders;

        this.shipperProviders.stream().forEach(p->shipperProvidersMap .put(/* your code for getting the key */, p));
    }

Gradle dependency hint:

compile group: 'org.springframework', name: 'spring-context', version: '5.1.9.RELEASE'
granadaCoder
  • 26,328
  • 10
  • 113
  • 146
jbppsu
  • 158
  • 4
  • I appreciate the attempt. But this is (1) setter based, not construction based (2) (at)Autowire ((at)Annotation) based not xml based. So I am still searching for an answer. Thank you. – granadaCoder Sep 14 '18 at 20:33
  • This actually helped me on another issue I was facing.......(not xml)...........so .. I upvoted. future readers. See also : https://stackoverflow.com/questions/17629761/strategy-pattern-with-spring-beans/20180762#20180762 – granadaCoder Aug 26 '19 at 00:23
0

I think I have something that works:

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

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util-2.5.xsd">


    <bean id="theLoggerBean"
        class="org.apache.commons.logging.impl.Log4JLogger">
        <constructor-arg value="log" />
    </bean>


    <bean id="fedExShipperBean"
        class="com.me.shipping.FedExShipper">
        <constructor-arg ref="theLoggerBean"></constructor-arg>
    </bean>

    <bean id="upsShipperBean"
        class="com.me.shipping.UpsShipper">
        <constructor-arg ref="theLoggerBean"></constructor-arg>
    </bean>

    <bean id="uspsShipperBean"
        class="com.me.shipping.UspsShipper">
        <constructor-arg ref="theLoggerBean"></constructor-arg>
    </bean>

    <util:map id="shipperInterfaceMap" key-type="java.lang.String"
        value-type="com.me.shipping.interfaces.ShipperInterface">
        <entry key="fedexFriendlyName" value-ref="fedExShipperBean" />
        <entry key="upsFriendlyName" value-ref="upsShipperBean" />
        <entry key="uspsFriendlyName" value-ref="uspsShipperBean" />
    </util:map>

    <bean id="orderProcessorImplBean"
        class="com.me.shipping.OrderProcessorImpl">
        <constructor-arg ref="theLoggerBean"></constructor-arg>
        <constructor-arg ref="shipperInterfaceMap"></constructor-arg>
    </bean>


</beans>

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<String, ShipperInterface> shipperInterfaceMap;


  public OrderProcessorImpl(Log lgr, java.util.Map<String, ShipperInterface> siMap) {

    if (null == lgr) {
      throw new IllegalArgumentException("Log is null");
    }

    if (null == siMap) {
      throw new IllegalArgumentException("Map<String, ShipperInterface> 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);
granadaCoder
  • 26,328
  • 10
  • 113
  • 146