0

I have a javafx application that uses spring boot. I want to display the beans being loaded at a splash screen.

How do I intercept the bean loading so I can get it's name?

I was previously pointed that it was already answered here: Print all the Spring beans that are loaded

But I want to know which bean is being loaded, not the beans that are already loaded.

Thiago Sayão
  • 1,871
  • 2
  • 21
  • 34

1 Answers1

1

You can use a BeanPostProcessor for this. It will be notified of any bean before and after initialisation:

public class PrintingBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Before init: " + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("After init: " + beanName);
        return bean;
    }

}

To register it, in my configuration class I simply do:

  @Bean
    public BeanPostProcessor postProcessor() {
        return new PrintingBeanPostProcessor();
    }

This then prints on startup:

Before init: resourceHandlerMapping
After init: resourceHandlerMapping
Before init: defaultServletHandlerMapping
After init: defaultServletHandlerMapping
Before init: mvcUriComponentsContributor
After init: mvcUriComponentsContributor
Before init: httpRequestHandlerAdapter
After init: httpRequestHandlerAdapter

(this is obviously just a fraction of what it prints)

Hope that helps,

Artur

pandaadb
  • 6,126
  • 2
  • 19
  • 37