Tuesday, September 4, 2012

Spring Interview Question - 2

This is the second set of questions that are generally asked during the interviews on Spring Framework. Some of these are advanced level questions like on the MVC architecture.

Question 1: What is BeanFactory?
Bean Factory is a factory class that contains the definition of multiple beans in itself, it can instantiate various beans when asked by the client. It has the capabilities to perform two major task during this process.
  • It is able to resolve the association between collaborating objects as the beans are instantiated. It in term removes the burden of the the configuration from the bean itself or from the calling client.
  • It can also call the various life cycle methods of beans and can calls the instantiation and destruction methods.
Question 2: What is ApplicationContext?
ApplicationContext is also like BeanFactory, the difference being that BeanFactory can only be created programmatically  whereas ApplciationContext can be created declaratively using ContextLoaders, for example a ContextLoaderListener. Apart from being able to perform the lifecycle control just like BeanFactory it can also load the various text message resources, file resources and can also pass the various events to beans that are registered as Listeners.

Question 3: What is Dependency Injection and Why is it becoming the de-facto standards for applications?
Dependency injection is the mechanism provided by framework where in the framework is responsible for creating the various dependencies and injecting them rather than the object creating the dependency. It becoming quite popular these days for the simple reason that it provides a loose coupling between various components. So, tomorrow if one of the dependency  changes rather then the whole application getting effect all the programmers need to do is just change the configuration as long as the changed bean also follows the same "contract" / interface as the previous one.

Question 4: What is the Spring's MVC architecture? or How does a web Request flows through Spring MVC?
Spring MVC architecture consists of the following things:
  • DispatcherServlet
  • LocaleResolver
  • MultipartResolver
  • ThemeResolver
  • HandlerMappings
  • Controller
  • ResultToViewNameTranslator
  • ViewResolver
The above components works as following to serve the request.
  • The DispatcherServlet receives the request from client
  • It uses the LocaleResolver to load any properties based on the Locale of the generated request.
  • It then uses the ThemeResolver to find any specific theme / template that might be used for that particular locale.
  • Then it find out whether the request has a multipart mime type then it initialises the file upload classes.
  • Then it uses the HandlerMappings to find the correct controller, where it uses the Model / Service Layer to execute the business logic. Before calling the controller method it has the capability to call the Pre adn Post Controller interceptor, which largely work as filters. 
  • Then it calls the ResultToViewNameTranslator to find the correct view.
  • Then the controller populates the Model and uses the ViewResolver to find the next view. It then combines the Model and the View to prepare the view, which is then passed to the container to return as Response.
Question 5: What are the main class in the Spring MVC architecture?
The main classes in the MVC architecture are as follows:
  1. DispatcherServlet
  2. MultipartResolver
  3. LocaleResolver
  4. ThemeResolver
  5. HandlerMapping
  6. HandlerAdapters
  7. HandlerExceptionResolver
  8. ResultToViewNameTranslator
  9. ViewResolver
Question 6: What is the typical lifecycle of a bean in the bean factory container?
Bean lifecycle in a bean container is as follows:
  • BeacFactory reads the bean definition from the xml configuration file.
  • BeanFactory instantiate the bean.
  • Using the dependency injection bean factory populates all the bean properties as specified in the bean definition.
  • If the bean implements BeanNameAware interface, then it calls the setBeanName() method passing the bean id.
  • If the bean implements BeanFactoryAware interface, then it calls the setBeanFactory() method passing an instance of itself.
  • If the bean has any BeanPostProcessors associated with itself it will call the postProcessBeforeTheInitialization() method.
  • If any init method is declare it will call that method and initialize the bean.
  • Finally if any BeanPostProcessor is associated with the bean then it will call the postProcessAfterInitialization().
Question 7: What are various Bean scopes available in Spring?
Spring supports the following scopes:
  • singleton - This is the default scope of a spring bean, it means the same instance is available through out the application
  • prototype - This means that the bean act as a template and a new instance will be injected for different beans.
  • request - The bean definition is available till the scope of an HTTP request. This is only applicable in case of the web-aware application context.
  • session - The bean is available till the scope of HTTP session. This is only applicable in case of the web-aware application context.
  • global-session - The bean is available till the scope of a global HTTP session. This is typically applicable in case of portlet context.
Question 8: Why is Spring MVC better than Struts ?