Results 1 to 2 of 2

Thread: Dependency Lookup with javax.inject.Provider is very very slow

Hybrid View

  1. #1
    Join Date
    Oct 2006
    Location
    Germany, Bonn
    Posts
    2

    Default Dependency Lookup with javax.inject.Provider is very very slow

    Hello,

    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:

    Code:
    @Service
    public class SomeService
    {
        // Using field injection to keep the example small
        @Autowired
        public Provider<Data> dataProvider;
    
        public void doSomething()
        {
            Data data = dataProvider.get();
            ...Do something with data...
        }
    }
    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.

    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?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    It does this because it looks up a bean by type (and before Spring 3.2) this call wasn't cached but iterated over all the beans. See this commit for more information.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •