Search code examples
springdependency-injectioninversion-of-control

Does spring container generate any event, whenever it get request for get an instance of bean?


I want to identify how many bean requests served by the spring container and which class generates which request. So is there any event, which spring container generates whenever it receives a request to get an instance of bean. Request might be through @Autowired or container.getBean() method.


Solution

  • You can try this:

    @Component
    public class ApplicationListenerAdapter implements ApplicationListener {
    
        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            //DO WHAT YOU WANT TO DO.
        }
    }
    

    Or you can handle a specific bean, using generics

    @Component
    public class ApplicationListenerAdapter implements ApplicationListener<YourCustomBean> {
    
        @Override
        public void onApplicationEvent(YourCustomBean event) {
            //DO WHAT YOU WANT TO DO.
        }
    }
    

    I'm not sure if it works with context.getBean.