springheap-memoryoql

OQL query to find Spring ApplicationContext holding specific bean


I use Spring Boot and have problem with beans instantiating (they created twice).

How can I find reference to ApplicationContext from bean reference in OQL query? In other word find out holding Context for given bean.

PS Basic troubleshooting query in VisualVM:

heap.objects("org.springframework.context.support.AbstractApplicationContext")
heap.objects("org.springframework.core.io.DefaultResourceLoader")

Solution

  • Find beans instances by bean name per ApplicationContext::

    map(heap.objects("org.springframework.context.support.AbstractApplicationContext"), function(it) {
      var fact = it.beanFactory;
      var tbl = fact.singletonObjects.table;
      var beans = map(filter(tbl, "it && /Step$/.test(it.key)"), "{key: it.key, val: it.val}");
      return {ctx: it, beans: beans};
    })
    

    Find beans instances by class name per ApplicationContext::

    map(heap.objects("org.springframework.context.support.AbstractApplicationContext"), function(it) {
      var fact = it.beanFactory;
      var tbl = fact.singletonObjects.table;
      var beans = map(filter(tbl, "it && /^com.bigbrother/.test(classof(it.val).name)"), "{key: it.key, val: it.val}");
      return {ctx: it, beans: beans};
    })
    

    If Spring change underlying implementation these queries should be adjusted. Look for fields:

    heap.objects("org.springframework.context.support.AbstractApplicationContext")