April 2018
Intermediate to advanced
246 pages
6h 11m
English
When you want to encapsulate the logic of creating the instance in a static way to custom methods, you can use a static factory method. In this case, Spring will use the Class attribute of <bean> to call the factory method and generate instances. Let's understand this by looking at the following example:
public class SearchableFactory { private static SearchableFactory searchableFactory; //Static factory method to get instance of Searchable Factory. public static SearchableFactory getSearchableFactory() { if(searchableFactory == null) { searchableFactory = new SearchableFactory(); } System.out.println("Factory method is used: getSearchableFactory() "); return searchableFactory; }}public class DIWithFactoryCheck {