We use the javax.inject.Provider stuff to inject scoped beans into singleton services. Works great but now we found that this is a massive performance problem. Here is what we do:
The Data object is coming from a factory bean (which is also a singleton). Our application has more than 1000 spring beans (Controllers, DAOs, Services). The dataProvider.get() line takes several seconds to complete. I debugged it to see what is happening inside and found out that Spring is iterating over ALL beans to find a suitable bean to return. And it does this EVERY TIME the get() method of the provider is called.
public class SomeService
// Using field injection to keep the example small
public Provider<Data> dataProvider;
public void doSomething()
Data data = dataProvider.get();
...Do something with data...
I don't understand why Spring is doing this. The factory bean which provides the Data object is also a singleton so why is Spring searching for this factory bean every time again and again? It could simply inject a Provider implementation which is already connected to this factory bean and then the get() Method of the provider could simply call the factory method of the already known factory bean, or not?