How do we tell Spring ServiceLocatorFactoryBean to provide the default instance of a service? I have a scenario like this.
package strategy;
import model.Document;
public interface IPrintStrategy {
public void print(Document document);
}
and 2 flavors of Strategy classes
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");
}
}
A Strategy Factory interface as below
package strategy;
public interface PrintStrategyFactory {
IPrintStrategy getStrategy(String strategyName);
}
and Spring config as below
<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">
<context:component-scan base-package="strategy">
<bean class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean" id="printStrategyFactory">
<property name="serviceLocatorInterface" value="strategy.PrintStrategyFactory">
</property></bean>
<alias alias="A4P" name="A4Portrait">
<alias alias="A4L" name="A4Landscape">
</alias></alias></context:component-scan></beans>
and my test class
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);
}
}
what will happen when I pass some text to the Factory like the last call
printStrategyFactory.getStrategy("Something").print(doc);
Is there a way to configure ServiceLocatorFactoryBean to send back the default instance of my Print Strategy, like the instance of the below class.
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");
}
}