Skip to content

Record the time it takes to run a method annotated with @PostConstruct [SPR-16287] #20834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Dec 11, 2017 · 1 comment
Labels
status: superseded An issue that has been superseded by another

Comments

@spring-projects-issues
Copy link
Collaborator

Tyler K Van Gorder opened SPR-16287 and commented

This is a small, very useful enhancement to help triage startup times of a Spring application. The methods annotated with @PostConstruct are all executed on the main thread during startup. In a large, legacy spring application there can be many such methods. Adding the ability to record how much time each method is contributing to the startup time is very useful. This can even be something that is only reported when debug logging is enabled. It might be more useful to report a warning if a @PostConstruct method exceeds a certain threshold (although this is not critical).

I have implemented this locally by making a change to the InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata.invokeInitMethods()

for (LifecycleElement element : initMethodsToIterate) {
	if (debug) {
		long start =  System.currentTimeMillis();
		logger.debug("Invoking init method on bean '" + beanName + "': " + element.getMethod());
		element.invoke(target);
		logger.debug("Init method on bean '" + beanName + "': " + element.getMethod() + " took " + (System.currentTimeMillis() - start) + "ms.");
	} else {
		element.invoke(target);
	}
}

Affects: 4.3.14

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.x Backlog milestone Jan 11, 2019
@bclozel
Copy link
Member

bclozel commented Apr 7, 2020

I'm closing this issue as it's been superseded by #24878. Thanks!

@bclozel bclozel closed this as completed Apr 7, 2020
@bclozel bclozel added status: superseded An issue that has been superseded by another and removed in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Apr 7, 2020
@jhoeller jhoeller removed this from the 5.x Backlog milestone Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another
Projects
None yet
Development

No branches or pull requests

3 participants