Spring MVC implementation principles

  1. Client sends request to dispacherServlet (dispenser)
  2. The dispacherservlet controller queries Handemapping to find the controller to handle the request
  3. After processing the request, the Controller returns to modelandView
  4. There is a dispacherservlet query that attempts to resolve the parser to find the view specified by ModelAndView
  5. The view is responsible for presenting the results to the client

Application scenarios for Spring AOP

  1. Performance testing
  2. Access control, security control
  3. Log management
  4. The transaction

The implementation principle of Spring AOP

AOP is based on dynamic proxy implementation

Three ways of agency

  • Static agent
  • JDK dynamic proxy
  • Additional agent

Spring MVC interceptor and handles global exceptions

The interceptor

  1. Implement the handlerInterceptor interface
  2. Override the preHandle method to verify that you are not an administrator
  3. Configure requests to be blocked in dispacter-servlet.xml (filters are in web.xml)

Handling global Exceptions

  1. Implement the handlerExceptionResolver interface
  2. Rewrite the resolveException method to return a ModelAndView object that gives a user friendly hint when the system throws an exception

Spring Boot is different from Spring MVC

  1. Spring MVC requires a large number of XML files to be configured, whereas Boot can be configured automatically
  2. Boot Embedded server
  3. Boot integrates a large number of third-party configuration libraries, out of the box

Spring characteristics of the boot

  1. Create a stand-alone Spring application
  2. Embedded Tomcat without the need to deploy a WAR file
  3. Simplifying Maven Configuration
  4. Automatic Spring configuration
  5. Provides production-ready functions such as metrics, health checks, and external configuration
  6. There is absolutely no code generation and no configuration requirement for XML

Common starter

    1.web
    2.test
    3.jpa
    4.redis
    5.mybatis
    6.jdbc
    7.mail
    8.thymeleaf
    9.security
    10.websocket
Copy the code

spring ioc

1. Three ways of injection

  • Property injection (setter methods)
  • Tectonic injection
  • Interface injection

2. Ioc container instantiates beans and establishes dependencies between beans through reflection mechanism

Beans are objects that are initialized, assembled, and managed by the Spring IOC container.

The difference and connection between interceptors and filters

The filter

Depending on the servlet container, almost all requests can be filtered. The disadvantage is that a filter instance can only be initialized once, usually doing some filtering to get the data we want

The interceptor

Can only intercept controller, dependency and Web framework so you can call Spring dependency injection, does not rely on servlet container, based on reflection mechanism, belongs to a kind of AOP application

Spring Boot automatic configuration principle

Adding the @SpringBootApplication or @enableAutoConfiguration annotation to the Spring startup class automatically reads the Spring. factories file in every stater in Meaven. This file configures all the beans that need to be created in the Spring container

Design patterns used in Spring

  • Bean singleton pattern
  • Beanfactory Factory mode
  • Aop dynamic proxy proxy pattern
  • Observer model

Spring uses the @Transaction annotation technology for declarative Transaction configuration.

The life cycle of spring beans

Spring only manages singleton beans for us. For prototype beans, spring does not manage them anymore and requires users to manage them manually

Life cycle diagram

The life cycle

instantiation

–> Fill the properties

–> Call various *Aware interfaces see the spring bean lifecycle for details

Call custom initialization methods

–> Bean is ready

Call the custom bean destruction method

Initialization and destruction

  1. Annotations: There are several phases in bean initialization, starting with annotations@PostConstruct.@PreDestroyTo be called during the bean creation and destruction phase
  2. Realize the initializingBean, DisposableBean interfaces can also realize the Bean initialization and destruction of operation.
  3. You can also customize the initialization and destruction methods

Implement the *Aware interface

* The Aware interface can be used to get some objects in Spring when initializing beans