Spring Interview Question

What is Spring MVC framework?
The Spring web MVC framework provides model-view-controller architecture and ready components that can be used to develop flexible and loosely coupled web applications. The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between model, view and controller parts of application. Spring framework provides lots of advantages over other MVC frameworks e.g. we can see as in below figure


• Clear separation of roles -- controller, validator, command object, form object, model object, DispatcherServlet, handler mapping, view resolver, etc.  Each role can   be fulfilled by a specialized object.

• Powerful and straightforward configuration of both framework and application classes as JavaBeans.

• Reusable business code -- no need for duplication.You can use existing business objects as command or form objects instead of mirroring them in order to extend a   particular framework  base class.

• Customizable binding and validation
• Customizable handler mapping and view resolution

• Customizable locale and theme resolution

• A JSP form tag library, introduced in Spring 2.0, that makes writing forms in JSP pages much easier. etc.

What is the front controller class of Spring MVC? 
A front controller() is defined as “a controller which handles all requests for a Web Application.” DispatcherServlet (actually a servlet) is the front controller in Spring MVC that intercepts every request and then dispatches/forwards requests to an appropriate controller.
When a web request is sent to a Spring MVC application, dispatcher servlet first receives the request. Then it organizes the different components configured in Spring’s web application context (e.g. actual request handler controller and view resolvers) or annotations present in the controller itself, all needed to handle the request.

Explain @Qualifier annotation with example ?
@Qualifier means, which bean is qualify to autowired on a field. The qualifier annotation helps disambiguate bean references when Spring would otherwise not be able to do so. See below example, it will autowired a “Employee” bean into customer’s employee property.
public class Customer{
 @Autowired
  private Employee employee;
}
And we have two bean definitions for Employee class.
<bean id="customer" class="com.java.common.Customer" />
<bean id="employeeA" class="com.java.common.Employee" >
    <property name="name" value="abc" />
</bean>
<bean id="employeeB" class="com.java.common.Employee" >
    <property name="name" value="Xyz" />
</bean>
Will Spring know which Employee bean should autowired? NO. When you run above example, it hits below exception:
Error thrown:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
    No unique bean of type [com.java.common. Employee] is defined:
        expected single matching bean but found 2: [employeeA, employeeB]
To fix above problem, you need @Quanlifier to tell Spring about which bean should autowired.
public class Customer
{
    @Autowired
    @Qualifier("employeeA")
    private Employee employee;
}

What are the benefits of Spring MVC over Struts.
Spring is a powerful Java application framework, used in a wide   range of Java applications. It provides enterprise services to Plain Old Java Objects POJOs). Spring uses dependency injection to achieve simplification and increase testability. Following are benefits over struts which makes Spring framework is most popular these days.
·        Spring offers better integration with view technologies other than JSP (Velocity / XSLT / Free Marker / XL etc.)
·        Spring Controllers are configured via IoC like any other objects. This makes them easy to test, and beautifully integrated with other objects managed by Spring.
·        No ActionForms. Bind directly to domain objects
·        More testable code (validation has no dependency on Servlet).
·        Spring MVC is truly view-agnostic. You don’t get pushed to use JSP if you don’t want to; you can use Velocity, XLST or other view technologies. If you want to use a custom view mechanism 
·        Spring, like WebWork, provides interceptors as well as controllers, making it easy to factor out behavior common to the handling of many requests.
·        Struts imposes dependencies on your Controllers (they must extend a Struts class), Spring doesn’t force you to do this although there are convenience Controller implementations that you can choose to extend.




No comments:

Post a Comment