Class BeanTimeStatistics

java.lang.Object
io.microsphere.spring.context.event.BeanTimeStatistics
All Implemented Interfaces:
BeanListener, EventListener, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanNameAware

public class BeanTimeStatistics extends Object implements BeanListener, org.springframework.beans.factory.BeanNameAware

BeanTimeStatistics is a BeanListener implementation that tracks and measures the time spent during various stages of a Spring Bean's lifecycle using a StopWatch instance.

This class provides detailed timing information for bean instantiation, initialization, destruction, and other key events in the Spring context. It is particularly useful for performance analysis and debugging.

Example Usage


 // Registering BeanTimeStatistics as a listener in a Spring context
 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
 context.register(BeanTimeStatistics.class);
 context.refresh();
 

Once registered, it will automatically begin measuring the following bean lifecycle events:

The collected timing data can be accessed via the getStopWatch() method, which returns a StopWatch instance containing all measured times.

Since:
1.0.0
Author:
Mercy
See Also:
  • Constructor Details

    • BeanTimeStatistics

      public BeanTimeStatistics()
  • Method Details

    • supports

      public boolean supports(String beanName)
      Description copied from interface: BeanListener
      Supports the bean to be listened or not
      Specified by:
      supports in interface BeanListener
      Parameters:
      beanName - the name of bean
      Returns:
      If false, any method of bean lifecycle will not be called
    • onBeanDefinitionReady

      public void onBeanDefinitionReady(String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition)
      Description copied from interface: BeanListener
      Handle the event when the BeanDefinition is ready(merged)
      Specified by:
      onBeanDefinitionReady in interface BeanListener
      Parameters:
      beanName - the bean name
      mergedBeanDefinition - the merged BeanDefinition
    • onBeforeBeanInstantiate

      public void onBeforeBeanInstantiate(String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition)
      Description copied from interface: BeanListener
      Handle the event before the bean instantiation
      Specified by:
      onBeforeBeanInstantiate in interface BeanListener
      Parameters:
      beanName - the bean name
      mergedBeanDefinition - the merged BeanDefinition
    • onBeforeBeanInstantiate

      public void onBeforeBeanInstantiate(String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, Constructor<?> constructor, Object[] args)
      Description copied from interface: BeanListener
      Handle the event before the bean instantiation using the constructor
      Specified by:
      onBeforeBeanInstantiate in interface BeanListener
      Parameters:
      beanName - the bean name
      mergedBeanDefinition - the merged BeanDefinition
      constructor - the constructor for bean instantiation
      args - the arguments of constructor
    • onBeforeBeanInstantiate

      public void onBeforeBeanInstantiate(String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, Object factoryBean, Method factoryMethod, Object[] args)
      Description copied from interface: BeanListener
      Handle the event before the bean instantiation using the factory method
      Specified by:
      onBeforeBeanInstantiate in interface BeanListener
      Parameters:
      beanName - the bean name
      mergedBeanDefinition - the merged BeanDefinition
      factoryBean - the factory bean(optional)
      factoryMethod - the factory Method for bean instantiation
    • onAfterBeanInstantiated

      public void onAfterBeanInstantiated(String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, Object bean)
      Description copied from interface: BeanListener
      Handle the event before after the bean instantiation
      Specified by:
      onAfterBeanInstantiated in interface BeanListener
      Parameters:
      beanName - the bean name
      mergedBeanDefinition - the merged BeanDefinition
      bean - the bean has been instantiated, not initialized yet
    • onBeanPropertyValuesReady

      public void onBeanPropertyValuesReady(String beanName, Object bean, org.springframework.beans.PropertyValues pvs)
      Description copied from interface: BeanListener
      Handle the event when the Beans' PropertyValues is ready
      Specified by:
      onBeanPropertyValuesReady in interface BeanListener
      Parameters:
      beanName - the bean name
      bean - the bean has been instantiated, not initialized yet
      pvs - the PropertyValues will be applied on bean
    • onBeforeBeanInitialize

      public void onBeforeBeanInitialize(String beanName, Object bean)
      Description copied from interface: BeanListener
      Handle the event before the bean initialization
      Specified by:
      onBeforeBeanInitialize in interface BeanListener
      Parameters:
      beanName - the bean name
      bean - the bean is not initialized yet
    • onAfterBeanInitialized

      public void onAfterBeanInitialized(String beanName, Object bean)
      Description copied from interface: BeanListener
      Handle the event after the bean initialization
      Specified by:
      onAfterBeanInitialized in interface BeanListener
      Parameters:
      beanName - the bean name
      bean - the bean has been initialized
    • onBeanReady

      public void onBeanReady(String beanName, Object bean)
      Description copied from interface: BeanListener
      Handle the event on the bean ready
      Specified by:
      onBeanReady in interface BeanListener
      Parameters:
      beanName - the bean name
      bean - the bean is prepared
    • onBeforeBeanDestroy

      public void onBeforeBeanDestroy(String beanName, Object bean)
      Description copied from interface: BeanListener
      Handle the event before the bean destroy
      Specified by:
      onBeforeBeanDestroy in interface BeanListener
      Parameters:
      beanName - the bean name
      bean - the bean is to be destroyed
    • onAfterBeanDestroy

      public void onAfterBeanDestroy(String beanName, Object bean)
      Description copied from interface: BeanListener
      Handle the event after the bean destroy
      Specified by:
      onAfterBeanDestroy in interface BeanListener
      Parameters:
      beanName - the bean name
      bean - the bean is destroyed
    • getStopWatch

      public io.microsphere.util.StopWatch getStopWatch()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • setBeanName

      public void setBeanName(String name)
      Specified by:
      setBeanName in interface org.springframework.beans.factory.BeanNameAware