Search code examples
springspring-bootinversion-of-control

What is the real life use-case of the applicationContext.getBean() in Spring Boot?


The applicationContext vs beanFactory seems to be very popular job interview question. Is there any real use case to call applicationContext.getBean(foo.class) or beanFactory.getBean(foo.class) explicitly in Spring Boot?

I have read some tutorials explaining the difference between ApplicationContext and BeanFactory, but I still don't see the real use-case. Why would any developer call getBean() explicitly?


Solution

  • BeanFactory is the parent interface that exposes all the basic methods of the spring container, which are closely related with beans (getBean(), containsBean(), isPrototype()...)

    Here a common scenario where it could be used, is for example in the main static method that spring boot starts, in case you want to do some action after the initialization of spring container has finished. Considering that you are inside a static method autowiring will not work. But with this BeanFactory you are able to manually do such action.

    Example

    @SpringBootApplication
    public class ServiceLauncher {
    
        public static void main(String[] args) {
            
            BeanFactory beanFactory = SpringApplication.run(ServiceLauncher.class, args);
            //At this point the Spring Beans Context has been loaded
            Activity activity = beanFactory.getBean(Activity.class); //So we are able to retrieve from the context a bean to do in this static class some more actions.
            activity.doSomething();         
        }   
    }
    

    ApplicationContext is an interface extending the above BeanFactory interface. It could be used again in the above scenario, but the reason it exists is that it exposes some more useful methods which could provide some extra functionality when needed. More commonly used are some interfaces that extend the ApplicationContext. Example the ConfigurableApplicationContext.

    Here common scenarios used are the refresh() of applicationContext, close() which closes the application context and also the registering of a shutdown hook when terminating with registerShutdownHook().

    Basically the registerShutdownHook() is commonly used because when the JVM is asked to terminate maybe you want to do a graceful shutdown, where the application context will not be closed immediately but will give you some control during shutdown.

    @Component
    public class Activity implements DisposableBean {
    
        @Override
        public void destroy() throws Exception {
            System.out.println("Destroying Activity component shutting down!!!");
        }
    } 
    

    and you register the hook with

        @SpringBootApplication
        public class ServiceLauncher {
        
            public static void main(String[] args) {            
                ConfigurableApplicationContext context   = SpringApplication.run(ServiceLauncher.class, args);
                context.registerShutdownHook();         
            }   
        }
    

    Those are just some of the most common uses I have seen in several applications and projects. But each separate method exposed by some interface could have it's own use case and that's why it exists there. I am confident though that just for interview questions, the interviewer will have in his mind the above 2 scenarios.