Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotation based ServiceLocatorFactoryBean?

I would like to implement Factory pattern in my project..i have gone through online resources and I came to know that spring ServiceLocatorFactoryBean should be implemented instead of normal java factory pattern....

i have followed this link but it is explained in xml based....can any one tell me how to do it using annotations based Factory pattern??

like image 766
kumar Avatar asked Nov 30 '15 07:11

kumar


People also ask

What is annotation based container configuration?

What is Spring Annotation Based Configuration? In Spring Framework annotation-based configuration instead of using XML for describing the bean wiring, you have the choice to move the bean configuration into component class. It is done by using annotations on the relevant class, method or the field declaration.

What is ServiceLocatorFactoryBean?

Spring's ServiceLocatorFactoryBean is a FactoryBean implementation that takes a service locator interface, Service Factory in service locator parlance, and returns a service requested by the client.


1 Answers

Spring Java Configuration ref guide @Configuration

Interface Parser.class

public interface Parser {
  void parse(String str);
}

Implementation for above interface.

JsonParser.java

public class JsonParser implements Parser {
  @Override
  public void parse(String str) {
     System.out.println("JsonParser.parse::" + str);
  }
}

XMLParser.java

public class XMLParser implements Parser{

  @Override
  public void parse(String str) {
     System.out.println("XMLParser.parse :: " + str);
  }
}

ParserFactory.java actual Factory interface.

public interface ParserFactory {
  public Parser getParser(ParserType parserType);
}

ParseType.java enum to specify parsing types(avoid typos and safe)

public enum ParserType {

JSON("jsonParser"), XML("xmlParser");

private final String value;

  ParserType(String input) {
     this.value = input;
  }

  public String getValue() {
     return this.value;
  }

  @Override
  public String toString() {
    return this.value;
  }
}

ParseService.java , where Business logic implemeted.

@Service
public class ParserService {

 @Autowired
 private ParserFactory parserFactory;

 public void doParse(String parseString, ParserType parseType) {
    Parser parser = parserFactory.getParser(parseType);
    System.out.println("ParserService.doParse.." + parser);
    parser.parse(parseString);
 }
}

Finally AppConfig.java Spring java configuration class, where all of my beans registered as container managed beans.

@Configuration
@ComponentScan(basePackages = {"<Your Package Name>"})
public class AppConfig {
 @Bean
 public FactoryBean serviceLocatorFactoryBean() {
    ServiceLocatorFactoryBean factoryBean = new ServiceLocatorFactoryBean();
    factoryBean.setServiceLocatorInterface(ParserFactory.class);
    return factoryBean;
 }

 @Bean(name = "jsonParser")
 @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
 public JsonParser jsonParser() {
    return new JsonParser();
 }

 @Bean(name = "xmlParser")
 @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
 public XMLParser xmlParser() {
    return new XMLParser();
 }
}

Now autowire ParserService bean in either controller or test classs, and invoke parese(String, ParseType) method to test.

Here is my test.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class ServiceLocatorFactoryExample {


 @Autowired
 private ParserService parserService;

 @Test
 public void testParserFactory() {
    parserService.doParse("Srilekha", ParserType.JSON);
    parserService.doParse("Srilekha", ParserType.XML);
 }
}
like image 170
Lovababu Avatar answered Sep 27 '22 22:09

Lovababu