Java Interview Questions - Spring (continuously updated)

What is Spring?

  1. The core of Spring is a lightweight (Lightweight) container (Container).
  2. Spring is a framework for implementing IoC (Inversion of Control) container and non-intrusive (No intrusive).
  3. Spring provides an implementation of the AOP (Aspect-oriented programming) concept.
  4. Spring provides support for Persistence and Transaction.
  5. Spring provides the implementation of the MVC Web framework and provides consistent model encapsulation for some commonly used enterprise service APIs (Application Interface).
  6. Spring provides solutions for integrating various existing frameworks (Structs, JSF, Hibernate, Ibatis, Webwork, etc.).

What is SpringMvc?

  It is a Spring-based MVC framework.

The entire execution process of SpringMvc:

  1. Initiate a request to the front controller (DispatcherServlet);
  2. DispatcherServlet requests HandlerMapping to find Handler (you can find it according to xml and annotations);
  3. HandlerMapping returns Handler to the forward DispatcherServlet;
  4. DispatcherServlet calls HandlerAdapter to execute Handler;
  5. HandlerAdapter 执行 Handler ;
  6. After the Handler is executed, it returns ModelAndView to the handler adapter;
  7. The processor adapter returns ModelAndView to the front controller (ModelAndView is the underlying object of SpringMvc, including model and view);
  8. The front-end controller requests the view parser to parse the view, and parses it into a real view (jsp) according to the logical view name;
  9. Front controller for view rendering

    Front Controller: DispatcherServlet
    Handler Mapper: HandlerMapping
    Handler Adapter: HandlerAdapter

Difference between SpringMvc and Struts2

  Struts2 is a class-level interception, a class corresponds to a request context, SpringMVC is a method-level interception, a method corresponds to a request context, and a method corresponds to a url at the same time, so from the architecture itself, SpringMVC can easily implement restful urls, The structure of struts2 is difficult to implement, because a method of Action in Struts2 can correspond to a url, but its class attributes are shared by all methods, so it is impossible to use annotations or other methods to identify the method to which it belongs.
  For the above reasons, SpringMVC's methods are basically independent, and they share the request response data exclusively. The request data is obtained through parameters, and the processing results are returned to the framework through ModelMap. Variables are not shared between methods, and Struts2 is messy. Although the methods are also independent, all their Action variables are shared, which will not affect the running of the program, but it will bring trouble to us when coding and reading the program. Every time a request comes, an Action is created, and an Action object corresponds to one request context.
  Because Struts2 needs to encapsulate each request, encapsulate the variables of the servlet life cycle such as request and session into a Map, supply each Action, and ensure thread safety, so in principle, it is more memory-intensive.
   In terms of interceptor implementation mechanism, Struts2 has its own interceptor mechanism, and SpringMVC uses an independent AOP method, which results in that the amount of configuration files of Struts2 is still larger than that of SpringMVC.
  The entrance of SpringMVC is servlet, and Struts2 is filter (it should be pointed out here that filter and servlet are different. In the past, filter was considered to be a special kind of servlet), which led to different mechanisms between the two, which involved servlet and filter. difference.
  SpringMVC integrates Ajax, which is very convenient to use. It can be implemented with only one annotation @ResponseBody, and then directly returns the response text. The Struts2 interceptor integrates Ajax. When processing in Action, you must generally install plug-ins or write code to integrate it. , it is relatively inconvenient to use.
  SpringMVC verification supports JSR303, which is relatively more flexible and convenient to handle, while Struts2 verification is more cumbersome and feels too annoying.
  Spring MVC and Spring are seamless. The management and security of this project is also higher than that of Struts2 (of course, Struts2 can also achieve the same effect as SpringMVC through different directory structures and related configurations, but there are many places that require xml configuration).
   In terms of design ideas, Struts2 is more in line with OOP programming ideas, and SpringMVC is more cautious and extends on servlets.
  SpringMVC development efficiency and performance are higher than Struts2.
  SpringMVC can be considered 100% zero configuration.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324516441&siteId=291194637