SpringIoC and SpringMVC Quick Start

More welcome attention to micro-channel public number: All food engineer Xiaohui ~

Spring advantage?

  1. Reducing the coupling between components, to achieve a decoupling between software layers
  2. You can use easily provide many services such as transaction management, messaging services, etc.
  3. Singleton container provides support
  4. Container provides AOP technology, use it very easy to implement, such as permission to intercept runtime monitoring and other functions
  5. Spring IoC approach reduces the complexity of the business object replacement
  6. Container offers numerous auxiliary class can accelerate the development of applications
  7. Spring for mainstream application framework provides integrated support, such as Hibernate, JPA, Mybatis etc.
  8. Spring design a low-invasive, low pollution code, and independent of the various application servers
  9. Spring's highly open, not mandatory application entirely dependent on Spring, developers are free to select some or all of the spring.

Why is this article not speak AOP?

IoC and AOP are two features of the Spring Framework, IoC and MVC processes are inseparable, can be seen as implement object-oriented programming; and AOP features are reflected in section oriented programming, but also complement the former, it is possible to split open follow-up to explain.

In this paper, the process SpringIoC and SpringMVC were simple explanations, more detailed process, welcome to see the author of the code base, which almost every line of code to do a detailed notes, with a request debug down the process to clear out.

Quick poke me, we go to learn ~

What is SpringIoC?

Inversion of Control IoC (Inversion of Control), is a design idea. The program does not IoC we use to create dependencies between objects and object-oriented programming object entirely hard-coded in the program, creating an object is controlled by its own program, while the use of Inversion of Control, will be transferred to a third party to create the object.

image

IoC is the core Spring Framework, using a variety of ways the perfect realization of IoC, you can use the XML configuration, you can also use annotations, a new version of Spring can achieve zero configuration IoC. Spring container during initialization before reading the configuration file, the tissue created objects into a container according to the configuration file or metadata, and then removed from the subject in need IoC container used in the program.

Bean using XML arranged, when the definition information is Bean and separation, and the use as annotations can be both combined into one, Bean definition information directly in the implementation class is defined as annotations, so as to achieve zero configuration purposes.

SpringIoC process?

  1. Base package scans. Reads the configuration file, and then scan the whole package configuration according to the scan path ,, and finally added to the class name with the fully qualified name after collection array
  2. All instances of the class annotated. Array set traversal step, the judgment on the annotations and instantiated class to class, called the class key, instance is value, which put in the hash table
  3. Dependency Injection. Step on the traverse hash table, get all the Fields on class, traversing Fields, Fields judgment on whether @Autowired types of notes, if it is, then put the assignment in this instance Map field
  4. Url address and mapping methods. The second step of traversing the hash table, if the Controller instance, acquire and Methods traversal, if the determination method comprising @RequestMapping annotation, if contained, its value is acquired, the value @RequestMapping annotation key value composed splicing @Controller , the current value as a method, the charged hanlerMap

  5. Reflection request call. (This step is already an MVC visible) over a url request, obtain its address, split, obtaining Controller example, obtaining Method instance, reflected executed Method

What is SpringMVC?

SpringMVC request frame is driven around the Servlet design, the request to the controller, then the model object dispatcher view to show the results of the request. Wherein the core classes is DispatcherServlet, it is a Servlet, the top layer is achieved Servlet interface.

Why SpringMVC?

Effect of the frame has been used to simplify programming:
servlet only doGet and doPost, a servlet class can handle a url-pattern.
SpringMVC class which can be processed by RequestMapping many requests, and a request to support Rest style, such as DELETE / PUT the like; SpringMVC mapping parameters can be packaged directly into the entity classes.

SpringMVC process

image

A flowchart illustrating:

  1. The user sends a request to the front-end controller DispatcherServlet.
  2. DispatcherServlet front controller calls the mapper processor HandlerMapping receipt of the request.
  3. The processor mapper HandlerMapping find specific processor request according Url, generates the object and a processor Handler interceptors HandlerIntercepter (if there is generated) collectively returned to the front controller DispatcherServlet.
  4. DispatcherServlet HandlerAdapter front controller Controller call processor through the processor adapter.
  5. Execution processor (Controller, also known as back-end)
  6. Processor Controller after the implementation of return ModelAndView.
  7. Processor Controller mapper HandlerAdapter processor execution result returned back to the front controller ModelAndView DispatcherServlet.
  8. The front controller DispatcherServlet pass ModelAnView view resolver ViewResolver.
  9. Back view resolver ViewResolver View parses specific view.
  10. A front end view of the controller DispatcherServlet View render view (i.e.: filling data into the model view)
  11. DispatcherServlet front controller in response to the user.

MVC process modules:

  1. DispatcherServlet: front-end controller (not require programmers to develop)
    a user request reaches the front end controller, which is equivalent in MVC C (Controller), DispatcherServlet center of the entire process is controlled by the other components of the processing which calls the user's request, the DispatcherServlet the presence of the reducing coupling between components.
    Effect: As receiving the request, the corresponding results, corresponding to the repeater, the central processor to reduce the degree of coupling between the other components.
  2. The HandlerMapping: a processor mapper (not require programmers to develop)
    the HandlerMapping responsible for finding Handler (i.e.: a processor) according to a user request, provide different SPRINGMVC mapper mapping implemented to achieve different ways, for example: profiles, to achieve Interface etc., annotation mode.
    Role: Find Handler according to the request Url
  3. Handler: a processor (need programmers)
    Handler Controller DispatcherServlet following the rear of the front controller, under the control of DispatcherServlet, Handler for specific user requests are processed.
    Since Handler to design a specific user service request, it generally requires the programmer to develop business needs Handler.
  4. HandlerAdapter: Processor Adapter
    through HandlerAdapter of processors, which is an application of the adapter mode, the processor may be more types of expansion performed by the adapter.
    Role: Handler to perform in accordance with specific rules (HandlerAdapter required by the rules)
  5. ViewResolver: view resolver (not require programmers)
    ViewResolver responsible for the processing result View to generate, according to ViewResolver first resolves the name into a physical view of the logical view name, i.e., the specific page address, then generates a view object View, View finally rendered the treatment results by showing the page to the user. View SpringMVC view framework provides many types, comprising: JSTLView, freemarkerView, pdfView like.
    Action: parsing a view, according to the logical name resolution to view the real view (view).
  6. View view (JSP programmers needed)
    View is an interface implementation class support different types of View (jsp, freemarker etc.) needs to show the data model to the user through a page by page or tab page template technology generally, the need to program members develop specific page based on business needs.

Heck, if my card lost.  Micro-letter search for "whole food engineer Xiaohui," I can still find

Guess you like

Origin www.cnblogs.com/mseddl/p/11416030.html