Class BeanTimeStatistics
- java.lang.Object
-
- io.microsphere.spring.context.event.BeanTimeStatistics
-
- All Implemented Interfaces:
BeanListener,java.util.EventListener,org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanNameAware
public class BeanTimeStatistics extends java.lang.Object implements BeanListener, org.springframework.beans.factory.BeanNameAware
BeanTimeStatisticsis aBeanListenerimplementation that tracks and measures the time spent during various stages of a Spring Bean's lifecycle using aStopWatchinstance.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:
onBeanDefinitionReady(String, RootBeanDefinition)- when a bean definition is readyonBeforeBeanInstantiate(String, RootBeanDefinition)- before a bean is instantiatedonAfterBeanInstantiated(String, RootBeanDefinition, Object)- after a bean is instantiatedonBeforeBeanInitialize(String, Object)- before a bean is initializedonAfterBeanInitialized(String, Object)- after a bean is initializedonBeanReady(String, Object)- when a bean is fully ready for useonBeforeBeanDestroy(String, Object)- before a bean is destroyedonAfterBeanDestroy(String, Object)- after a bean is destroyed
The collected timing data can be accessed via the
getStopWatch()method, which returns aStopWatchinstance containing all measured times.- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
StopWatch,BeanListener,EventPublishingBeanInitializer
-
-
Constructor Summary
Constructors Constructor Description BeanTimeStatistics()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description io.microsphere.util.StopWatchgetStopWatch()voidonAfterBeanDestroy(java.lang.String beanName, java.lang.Object bean)Handle the event after the bean destroyvoidonAfterBeanInitialized(java.lang.String beanName, java.lang.Object bean)Handle the event after the bean initializationvoidonAfterBeanInstantiated(java.lang.String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, java.lang.Object bean)Handle the event before after the bean instantiationvoidonBeanDefinitionReady(java.lang.String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition)Handle the event when theBeanDefinitionis ready(merged)voidonBeanPropertyValuesReady(java.lang.String beanName, java.lang.Object bean, org.springframework.beans.PropertyValues pvs)Handle the event when the Beans'PropertyValuesis readyvoidonBeanReady(java.lang.String beanName, java.lang.Object bean)Handle the event on the bean readyvoidonBeforeBeanDestroy(java.lang.String beanName, java.lang.Object bean)Handle the event before the bean destroyvoidonBeforeBeanInitialize(java.lang.String beanName, java.lang.Object bean)Handle the event before the bean initializationvoidonBeforeBeanInstantiate(java.lang.String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition)Handle the event before the bean instantiationvoidonBeforeBeanInstantiate(java.lang.String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, java.lang.Object factoryBean, java.lang.reflect.Method factoryMethod, java.lang.Object[] args)Handle the event before the bean instantiation using the factory methodvoidonBeforeBeanInstantiate(java.lang.String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, java.lang.reflect.Constructor<?> constructor, java.lang.Object[] args)Handle the event before the bean instantiation using theconstructorvoidsetBeanName(java.lang.String name)booleansupports(java.lang.String beanName)Supports the bean to be listened or notjava.lang.StringtoString()
-
-
-
Method Detail
-
supports
public boolean supports(java.lang.String beanName)
Description copied from interface:BeanListenerSupports the bean to be listened or not- Specified by:
supportsin interfaceBeanListener- Parameters:
beanName- the name of bean- Returns:
- If
false, any method of bean lifecycle will not be called
-
onBeanDefinitionReady
public void onBeanDefinitionReady(java.lang.String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition)Description copied from interface:BeanListenerHandle the event when theBeanDefinitionis ready(merged)- Specified by:
onBeanDefinitionReadyin interfaceBeanListener- Parameters:
beanName- the bean namemergedBeanDefinition- the mergedBeanDefinition
-
onBeforeBeanInstantiate
public void onBeforeBeanInstantiate(java.lang.String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition)Description copied from interface:BeanListenerHandle the event before the bean instantiation- Specified by:
onBeforeBeanInstantiatein interfaceBeanListener- Parameters:
beanName- the bean namemergedBeanDefinition- the mergedBeanDefinition
-
onBeforeBeanInstantiate
public void onBeforeBeanInstantiate(java.lang.String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, java.lang.reflect.Constructor<?> constructor, java.lang.Object[] args)Description copied from interface:BeanListenerHandle the event before the bean instantiation using theconstructor- Specified by:
onBeforeBeanInstantiatein interfaceBeanListener- Parameters:
beanName- the bean namemergedBeanDefinition- the mergedBeanDefinitionconstructor- theconstructorfor bean instantiationargs- the arguments ofconstructor
-
onBeforeBeanInstantiate
public void onBeforeBeanInstantiate(java.lang.String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, java.lang.Object factoryBean, java.lang.reflect.Method factoryMethod, java.lang.Object[] args)Description copied from interface:BeanListenerHandle the event before the bean instantiation using the factory method- Specified by:
onBeforeBeanInstantiatein interfaceBeanListener- Parameters:
beanName- the bean namemergedBeanDefinition- the mergedBeanDefinitionfactoryBean- the factory bean(optional)factoryMethod- the factoryMethodfor bean instantiation
-
onAfterBeanInstantiated
public void onAfterBeanInstantiated(java.lang.String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, java.lang.Object bean)Description copied from interface:BeanListenerHandle the event before after the bean instantiation- Specified by:
onAfterBeanInstantiatedin interfaceBeanListener- Parameters:
beanName- the bean namemergedBeanDefinition- the mergedBeanDefinitionbean- the bean has been instantiated, not initialized yet
-
onBeanPropertyValuesReady
public void onBeanPropertyValuesReady(java.lang.String beanName, java.lang.Object bean, org.springframework.beans.PropertyValues pvs)Description copied from interface:BeanListenerHandle the event when the Beans'PropertyValuesis ready- Specified by:
onBeanPropertyValuesReadyin interfaceBeanListener- Parameters:
beanName- the bean namebean- the bean has been instantiated, not initialized yetpvs- thePropertyValueswill be applied on bean
-
onBeforeBeanInitialize
public void onBeforeBeanInitialize(java.lang.String beanName, java.lang.Object bean)Description copied from interface:BeanListenerHandle the event before the bean initialization- Specified by:
onBeforeBeanInitializein interfaceBeanListener- Parameters:
beanName- the bean namebean- the bean is not initialized yet
-
onAfterBeanInitialized
public void onAfterBeanInitialized(java.lang.String beanName, java.lang.Object bean)Description copied from interface:BeanListenerHandle the event after the bean initialization- Specified by:
onAfterBeanInitializedin interfaceBeanListener- Parameters:
beanName- the bean namebean- the bean has been initialized
-
onBeanReady
public void onBeanReady(java.lang.String beanName, java.lang.Object bean)Description copied from interface:BeanListenerHandle the event on the bean ready- Specified by:
onBeanReadyin interfaceBeanListener- Parameters:
beanName- the bean namebean- the bean is prepared
-
onBeforeBeanDestroy
public void onBeforeBeanDestroy(java.lang.String beanName, java.lang.Object bean)Description copied from interface:BeanListenerHandle the event before the bean destroy- Specified by:
onBeforeBeanDestroyin interfaceBeanListener- Parameters:
beanName- the bean namebean- the bean is to be destroyed
-
onAfterBeanDestroy
public void onAfterBeanDestroy(java.lang.String beanName, java.lang.Object bean)Description copied from interface:BeanListenerHandle the event after the bean destroy- Specified by:
onAfterBeanDestroyin interfaceBeanListener- Parameters:
beanName- the bean namebean- the bean is destroyed
-
getStopWatch
public io.microsphere.util.StopWatch getStopWatch()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
setBeanName
public void setBeanName(java.lang.String name)
- Specified by:
setBeanNamein interfaceorg.springframework.beans.factory.BeanNameAware
-
-