Spring MVC implementation principles
- Client sends request to dispacherServlet (dispenser)
- The dispacherservlet controller queries Handemapping to find the controller to handle the request
- After processing the request, the Controller returns to modelandView
- There is a dispacherservlet query that attempts to resolve the parser to find the view specified by ModelAndView
- The view is responsible for presenting the results to the client
Application scenarios for Spring AOP
- Performance testing
- Access control, security control
- Log management
- 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
- Implement the handlerInterceptor interface
- Override the preHandle method to verify that you are not an administrator
- Configure requests to be blocked in dispacter-servlet.xml (filters are in web.xml)
Handling global Exceptions
- Implement the handlerExceptionResolver interface
- 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
- Spring MVC requires a large number of XML files to be configured, whereas Boot can be configured automatically
- Boot Embedded server
- Boot integrates a large number of third-party configuration libraries, out of the box
Spring characteristics of the boot
- Create a stand-alone Spring application
- Embedded Tomcat without the need to deploy a WAR file
- Simplifying Maven Configuration
- Automatic Spring configuration
- Provides production-ready functions such as metrics, health checks, and external configuration
- 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
- Annotations: There are several phases in bean initialization, starting with annotations
@PostConstruct
.@PreDestroy
To be called during the bean creation and destruction phase - Realize the initializingBean, DisposableBean interfaces can also realize the Bean initialization and destruction of operation.
- 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