title: ServiceLocatorFactoryBean Default instance
body: <p>How do we tell Spring ServiceLocatorFactoryBean to provide the default instance of a service?
  I have a scenario like this.</p>
  
  <pre><code>package strategy;
  
  import model.Document;
  
  public interface IPrintStrategy {
  public void print(Document document);
  }
  </code></pre>
  
  <p>and 2 flavors of Strategy classes</p>
  
  <pre><code>package strategy;
  
  import model.Document;
  
  import org.springframework.stereotype.Component;
  
  @Component("A4Landscape")
  public class PrintA4LandscapeStrategy implements IPrintStrategy{
  
   @Override
   public void print(Document document) {
    System.out.println("Doing stuff to print an A4 landscape document");
   }
  
  }
  
  
  package strategy;
  
  import model.Document;
  
  import org.springframework.stereotype.Component;
  
  @Component("A5Landscape")
  public class PrintA5LandscapeStrategy implements IPrintStrategy{
  
   @Override
   public void print(Document document) {
    System.out.println("Doing stuff to print an A5 landscape document");
   }
  
  }
  </code></pre>
  
  <p>A Strategy Factory interface as below</p>
  
  <pre><code>package strategy;
  
  public interface PrintStrategyFactory {
  
   IPrintStrategy getStrategy(String strategyName);
  </code></pre>
  
  <p>}</p>
  
  <p>and Spring config as below</p>
  
  <pre><code>&lt;beans xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
  
   &lt;context:component-scan base-package="strategy"&gt;
  
    &lt;bean class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean" id="printStrategyFactory"&gt;
    &lt;property name="serviceLocatorInterface" value="strategy.PrintStrategyFactory"&gt;
   &lt;/property&gt;&lt;/bean&gt;
  
   &lt;alias alias="A4P" name="A4Portrait"&gt;
   &lt;alias alias="A4L" name="A4Landscape"&gt;
  &lt;/alias&gt;&lt;/alias&gt;&lt;/context:component-scan&gt;&lt;/beans&gt;
  </code></pre>
  
  <p>and my test class</p>
  
  <pre><code>import model.Document;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
  import org.testng.annotations.Test;
  
  import strategy.PrintStrategyFactory;
  
  
  @ContextConfiguration(locations = {"classpath:/spring-config.xml"})
  public class SpringFactoryPatternTest extends AbstractTestNGSpringContextTests{
  
   @Autowired
   private PrintStrategyFactory printStrategyFactory;
  
   @Test
   public void printStrategyFactoryTest(){
    Document doc = new Document();
  
    printStrategyFactory.getStrategy("A4L").print(doc);
    printStrategyFactory.getStrategy("A5L").print(doc);
  
    printStrategyFactory.getStrategy("Something").print(doc);
  
   }
  }
  </code></pre>
  
  <p>what will happen when I pass some text to the Factory like the last call </p>
  
  <pre><code>  printStrategyFactory.getStrategy("Something").print(doc);
  </code></pre>
  
  <p>Is there a way to configure ServiceLocatorFactoryBean to send back the default instance of my Print Strategy, like the instance of the below class.</p>
  
  <pre><code>package strategy;
  
  import model.Document;
  
  import org.springframework.stereotype.Component;
  
  @Component("invalid")
  public class InvalidLandscapeStrategy implements IPrintStrategy{
  
   @Override
   public void print(Document document) {
    System.out.println("INVALID DOCUMENT STRATEGY");
   }
  
  }
  </code></pre>