When to use javax.inject.Provider in Spring?-Collection of common programming errors

This interface is equivalent to org.springframework.beans.factory.ObjectFactory that is tipically used to avoid BeanFactory.getBean() calls in client code when looking for prototype instances. Often used with ObjectFactoryCreatingFactoryBean to get protoypes beans sourced by the BeanFactory.

example from ObjectFactoryCreatingFactoryBean javadocs:



   
   

   
     
   

   
     
   


With Providers, you can use the ProviderCreatingFactoryBean instead.

Other option to solve the same problem, (using inheritance instead composition) is the lookup method injection