Announcement Announcement Module
No announcement yet.
getBean performance in case of hierarchical contexts Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • getBean performance in case of hierarchical contexts


    Lookup of bean using method getBean of ApplicationContext is by one order slower when looking up from hierarchical context (child context derives one parent context) than when looking up from flat context(no parent contexts). This is the case when the bean is defined in parent context.
    Little effort of profiling showed that this performance decrease is caused by naive implementation of BeanFactory.getBean(String name, Class requiredType, Object[] args):
    1. Implementation looks up for a bean in a child context
    2. If no bean is found, then an exception is thrown witch is caught. The exception handler looks up for a bean in a parent context.
    3. The bean is found and returned.

    The problem is that NoSuchBeanDefinitionException is very costly to construct at least as it is constructed now. The exception is initialized using String returned from call to DefaultListableBeanFactory.toString(). Construction of the String takes time - it is proportional to the "size" of context - sometimes it can be quite big.

    BTW. Spring 1.2.5 version was considered.

    Are you going to improve performance of getBean method?

    Best Regards,

    Mindaugas M

  • #2
    Thank you for the comment and analysis. It is very helpful.

    We will address this issue ASAP, which means that it will make Spring 2.0 M2, and hopefully also the forthcoming 1.2.7 maintenance release.


    • #3
      P.S. In general it is better to raise such an issue as an enhancement request in our JIRA issue tracker. However, we appreciate your taking the time to feed back.


      • #4
        Btw this issue should already have been resolved in 1.2.6, which has been released.


        • #5
          Mayank S

          I am using Spring Version 2.5.4.
          I have two questions

          1. Does getBean() method of AbstractbeanFactory class has performance issue?
          If not, then what is the expected time in which it fetches bean?

          2. When server is started. On first hit, I am facing performance issue when using getBean() method to instantiate Bean.
          Is there any other way apart from using getBean() method through which I can instantiate bean ?