Spring Dynamic Factory Bean

In this tutorial, we illustrate how to create a dynamic factory bean in Spring.

Suppose we have a file converter application which converts files based on a specific conversion type selected by the users. Our application would hold several conversion mechanisms and should be implemented in a way that dynamically instantiates the corresponding converter based on the selected conversion type.

1- FileConverterFactory

To prevent using several if/else statements each time we convert a file, we create a dynamic factory bean which instantiates all the converter beans at the startup of the application and saves them in a cached HashMap, each bean paired with its conversion type.

Now each time an application receives a conversion request, it gets the corresponding converter bean from the factory using the requested conversion type.

Our factory bean would look like the following:

Annotating the converters field with @Autowired informs the Spring framework to instantiate all the implementation classes of type FileConverter at the startup of the application.

Annotating the initConverterCache() method with @PostConstruct, instructs the Spring framework to call this method just after executing the @Autowired annotation, this method iterates over the converters and saves them in a cached HashMap called fileConverterCache, each paired with its conversion type.

The getConverter() method is exposed publicly to other classes which need to get an instance of a converter based on a specific conversion type.

Hussein Terek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

Leave a Reply

avatar