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:
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 a
StopWatch
instance containing all measured times.
- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
-
StopWatch
BeanListener
EventPublishingBeanInitializer
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionio.microsphere.util.StopWatch
void
onAfterBeanDestroy
(String beanName, Object bean) Handle the event after the bean destroyvoid
onAfterBeanInitialized
(String beanName, Object bean) Handle the event after the bean initializationvoid
onAfterBeanInstantiated
(String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, Object bean) Handle the event before after the bean instantiationvoid
onBeanDefinitionReady
(String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition) Handle the event when theBeanDefinition
is ready(merged)void
onBeanPropertyValuesReady
(String beanName, Object bean, org.springframework.beans.PropertyValues pvs) Handle the event when the Beans'PropertyValues
is readyvoid
onBeanReady
(String beanName, Object bean) Handle the event on the bean readyvoid
onBeforeBeanDestroy
(String beanName, Object bean) Handle the event before the bean destroyvoid
onBeforeBeanInitialize
(String beanName, Object bean) Handle the event before the bean initializationvoid
onBeforeBeanInstantiate
(String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition) Handle the event before the bean instantiationvoid
onBeforeBeanInstantiate
(String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, Object factoryBean, Method factoryMethod, Object[] args) Handle the event before the bean instantiation using the factory methodvoid
onBeforeBeanInstantiate
(String beanName, org.springframework.beans.factory.support.RootBeanDefinition mergedBeanDefinition, Constructor<?> constructor, Object[] args) Handle the event before the bean instantiation using theconstructor
void
setBeanName
(String name) boolean
Supports the bean to be listened or nottoString()
-
Constructor Details
-
BeanTimeStatistics
public BeanTimeStatistics()
-
-
Method Details
-
supports
Description copied from interface:BeanListener
Supports the bean to be listened or not- Specified by:
supports
in interfaceBeanListener
- 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 theBeanDefinition
is ready(merged)- Specified by:
onBeanDefinitionReady
in interfaceBeanListener
- Parameters:
beanName
- the bean namemergedBeanDefinition
- the mergedBeanDefinition
-
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 interfaceBeanListener
- Parameters:
beanName
- the bean namemergedBeanDefinition
- the mergedBeanDefinition
-
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 theconstructor
- Specified by:
onBeforeBeanInstantiate
in interfaceBeanListener
- Parameters:
beanName
- the bean namemergedBeanDefinition
- the mergedBeanDefinition
constructor
- theconstructor
for bean instantiationargs
- the arguments ofconstructor
-
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 interfaceBeanListener
- Parameters:
beanName
- the bean namemergedBeanDefinition
- the mergedBeanDefinition
factoryBean
- the factory bean(optional)factoryMethod
- the factoryMethod
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 interfaceBeanListener
- Parameters:
beanName
- the bean namemergedBeanDefinition
- the mergedBeanDefinition
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 interfaceBeanListener
- Parameters:
beanName
- the bean namebean
- the bean has been instantiated, not initialized yetpvs
- thePropertyValues
will be applied on bean
-
onBeforeBeanInitialize
Description copied from interface:BeanListener
Handle the event before the bean initialization- Specified by:
onBeforeBeanInitialize
in interfaceBeanListener
- Parameters:
beanName
- the bean namebean
- the bean is not initialized yet
-
onAfterBeanInitialized
Description copied from interface:BeanListener
Handle the event after the bean initialization- Specified by:
onAfterBeanInitialized
in interfaceBeanListener
- Parameters:
beanName
- the bean namebean
- the bean has been initialized
-
onBeanReady
Description copied from interface:BeanListener
Handle the event on the bean ready- Specified by:
onBeanReady
in interfaceBeanListener
- Parameters:
beanName
- the bean namebean
- the bean is prepared
-
onBeforeBeanDestroy
Description copied from interface:BeanListener
Handle the event before the bean destroy- Specified by:
onBeforeBeanDestroy
in interfaceBeanListener
- Parameters:
beanName
- the bean namebean
- the bean is to be destroyed
-
onAfterBeanDestroy
Description copied from interface:BeanListener
Handle the event after the bean destroy- Specified by:
onAfterBeanDestroy
in interfaceBeanListener
- Parameters:
beanName
- the bean namebean
- the bean is destroyed
-
getStopWatch
public io.microsphere.util.StopWatch getStopWatch() -
toString
-
setBeanName
- Specified by:
setBeanName
in interfaceorg.springframework.beans.factory.BeanNameAware
-