Fundamentals of Spring


1. Lifecycle of Spring MVC

  • The client sends a request to web container in the form of http request.
  • This incoming request is intercepted by Front controller (DispatcherServlet) and degates the control to appropriate Handler Mappings.
  • With the help of Handler Mappings, the DispatcherServlet dispatches the request to appropriate Controller.
  • The Controller tries to process the request and returns the Model and View object (ModelAndView instance) to the Front Controller.
  • The Front Controller resolves the View by consulting the View Resolver object.
  • The selected view is then rendered back to client 







2. What is Inversion of Control / dependency injection?

The basic principle behind Dependency Injection (DI) is that objects define their dependencies (that is to say the other objects they work with) only through constructor arguments, arguments to a factory method, or properties which are set on the object instance after it has been constructed or returned from a factory method. Then, it is the job of the container to actually inject those dependencies when it creates the bean. This is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself being in control of instantiating or locating its dependencies on its own using direct construction of classes, or something like the Service Locator pattern.
It becomes evident upon usage that code gets much cleaner when the DI principle is applied, and reaching a higher grade of decoupling is much easier when beans do not look up their dependencies, but are provided with them (and additionally do not even know where the dependencies are located and of what actual class they are).

3. What are types of dependency injections? (Constructor & Property based)

DI exists in two major variants, namely Setter Injection, and Constructor Injection.
Setter-based DI is realized by calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
Find below an example of a class that can only be dependency injected using pure setter injection. Note that there is nothing special about this class... it is plain old Java.


public class SimpleMovieLister {

    // the SimpleMovieLister has a dependency on the MovieFinder
    private MovieFinder movieFinder;

    // a setter method so that the Spring container can 'inject' a MovieFinder
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
    
    // business logic that actually 'uses' the injected MovieFinder is omitted...
}

Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator. Additionally, calling a static factory method with specific arguments to construct the bean, can be considered almost equivalent, and the rest of this text will consider arguments to a constructor and arguments to a static factory method similarly.
Find below an example of a class that could only be dependency injected using constructor injection. Again, note that there is nothing special about this class.
public class SimpleMovieLister {

    // the SimpleMovieLister has a dependency on the MovieFinder
    private MovieFinder movieFinder;

    // a constructor so that the Spring container can 'inject' a MovieFinder
    public SimpleMovieLister(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
    
    // business logic that actually 'uses' the injected MovieFinder is omitted...
}

4. Handler mapping

The DispatcherServlet enables the DefaultAnnotationHandlerMapping, which looks for @RequestMappingannotations on @Controllers. Typically, you do not need to override this default mapping, unless you need to override the default property values. These properties are:interceptors
List of interceptors to use. HandlerInterceptors are discussed in Section 15.4.1, “Intercepting requests - the HandlerInterceptor interface”.
defaultHandler
Default handler to use, when this handler mapping does not result in a matching handler.orderBased on the value of the order property (see the org.springframework.core.Ordered interface), Spring sorts all handler mappings available in the context and applies the first matching handler.
alwaysUseFullPath
If true , Spring uses the full path within the current servlet context to find an appropriate handler. If false (the default), the path within the current servlet mapping is used. For example, if a servlet is mapped using /testing/* and the alwaysUseFullPath property is set to true, /testing/viewPage.html is used, whereas if the property is set to false, /viewPage.html is used.
urlDecode
Defaults to true, as of Spring 2.5. If you prefer to compare encoded paths, set this flag to false. However, the HttpServletRequest always exposes the servlet path in decoded form. Be aware that the servlet path will not match when compared with encoded paths.
lazyInitHandlers
Allows lazy initialization of singleton handlers (prototype handlers are always lazy-initialized). The default value is false.

5. Different type of controllers 

Controllers provide access to the application behavior that you typically define through a service interface. Controllers interpret user input and transform it into a model that is represented to the user by the view
AbstractFormController: Form controller that auto-populates a form bean from the request. This, either using a new bean instance per request, or using the same bean when the sessionForm property has been set to true.
SimpleFormColtroller: Concrete FormController implementation that provides configurable form and success views, and an onSubmit chain for convenient overriding. Automatically resubmits to the form view in case of validation errors, and renders the success view in case of a valid submission.

6. AOP in spring 

AOP in Spring is an enabling technology that allows Spring to provide declarative transaction management without EJB; or use the full power of the Spring AOP framework to implement custom aspects.

7. How to configure multi-langual in Spring

org.springframework.context.support.ResourceBundle MessageSource can be used to inject multilingual Properties in Spring.

8. What are the Bean scopes supported in Spring 

The scopes supported out of the box are listed below:
Scope
Description
Scopes a single bean definition to a single object instance per Spring IoC container.
Scopes a single bean definition to any number of object instances.
Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.

9. Components of Spring


References

Comments

Popular posts from this blog

jQgrid reload with new data

Rich Client based UI technologies- A Comparison of Thick Client UI tools

OSS and BSS Systems