Java Spring Bean constructor call stack, backtrace

Easy wasy to print Java call stack or backtrace:
Implement a function and throw an Exception.
Call the function in the place you want to know its backtrace or callstack
And Catch the Exception then
Call Exception.getStackTrace

class MyService {

    public static void f() throws Exception {
        throw new Exception();
    }

    void forTest() {
        try {
            f();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Via this solution, we can get Bean constructor call stack easily.

[tomcat:launchProperties]       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[tomcat:launchProperties]       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[tomcat:launchProperties]       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[tomcat:launchProperties]       at java.lang.reflect.Method.invoke(Method.java:498)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1178)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1072)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
[tomcat:launchProperties]       at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1136)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064)
[tomcat:launchProperties]       at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
[tomcat:launchProperties]       at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
[tomcat:launchProperties]       at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:364)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1269)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:551)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
[tomcat:launchProperties]       at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
[tomcat:launchProperties]       at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
[tomcat:launchProperties]       at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
[tomcat:launchProperties]       at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
[tomcat:launchProperties]       at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
[tomcat:launchProperties]       at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
[tomcat:launchProperties]       at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4851)
[tomcat:launchProperties]       at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
[tomcat:launchProperties]       at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
[tomcat:launchProperties]       at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
[tomcat:launchProperties]       at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
[tomcat:launchProperties]       at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[tomcat:launchProperties]       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[tomcat:launchProperties]       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[tomcat:launchProperties]       at java.lang.Thread.run(Thread.java:748)

Subscribe to Post, Code and Quiet Time.

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe