Programming architecture (08): spring Mvc. Boot framework

Source code of this article: GitHub · click here | gitee · click here

1、 Spring framework

1. Framework overview

Spring is an open source framework. One of the main advantages of the framework is its layered architecture. The layered architecture allows users to choose which component to use, and provides an integrated framework for J2EE application development. Spring uses basic JavaBeans to do things that previously could only be done by EJBs. Spring is a layered lightweight open source framework.

Basic features: layered architecture, high cohesion and low coupling, support AOP programming, transaction management, integration testing and integration of various frameworks.

2. Core components

Core container: contains the functions of bean creation, configuration, management, etc.

AOP aspect programming: can help application decoupling.

Data access: it integrates JDBC, commonly used Dao layer framework, hibernate, mybatis, etc.

Web programming: MVC framework of integrated process, which realizes the separation of interface logic and application program.

3. Bean object understanding

Spring container is responsible for creating, assembling and setting properties, and then managing the objects in the whole life cycle, which is called bean objects.

Assembly method: XML format, annotation scanning, java code assembly.

Scope: used to determine the number of bean instances created by spring, such as singleton bean and prototype bean. Singleton default single instance, prototype multiple instances, request request, session level, global session.

Life cycle: instantiation, attribute loading, management before and after initialization, and destruction.

4. Common core notes

Controller: mark a class as handler, which is based on @ mapping related annotations (@ getmapping, @ postmapping, @ putmapping, @ deletemapping) to associate the mapping relationship between the request and the controller method, so that the controller can be accessed by the request.

Requestmapping: Annotation for processing request address mapping, which can be applied to classes or methods. Used on a class to indicate that all methods in the class that respond to requests take the address marked on the class as the parent path.

Resource: it is automatically injected according to byname, and the package javax needs to be imported annotation. Resource。@ Resource has two important attributes: name and type. Spring resolves the name attribute of the @ resource annotation to the name of the bean, while the type attribute resolves to the type of the bean.

Service: it can replace the bean management of specific configuration files. The defined beans are singleton by default, and the default name is the class name with lowercase initials.

5. IOC and di thought

IOC container

The object coupling relationship in Java system is very complex. This is the reason why the modules of the system depend on each other and the mutual call requests between microservice modules. Reducing the coupling between system modules, objects and micro services is one of the core problems of software engineering. Because the core idea of spring framework is IOC control inversion, which is used to realize decoupling between objects.

Dependency injection

The action of IOC to directly establish a relationship with an object is called Di dependency injection; Dependency: object a needs to use the function of object B, so object a depends on object B. Injection: instantiate object B in object a to use the function of object B. this action is called injection.

6. AOP section programming

It is a technology to realize the unified maintenance of program functions through precompiled mode and runtime dynamic agent. Core role: it can isolate each part of business logic, reduce the coupling between each part of business logic, and improve the reusability and development efficiency of programs. AOP provides a new scheme to replace inheritance and delegation, and it is more concise and clear to use. It is a hot concept in software development.

Implementation methods: JDK dynamic proxy, cglib bytecode enhancement, spring semi-automatic proxy, spring fully automatic proxy.

7. Transaction management

Transaction refers to a series of operations (SQL statements) executed as a single logical work unit. These operations are either successful or unsuccessful. The essence of spring transaction management is to encapsulate the operations supported by the database for transactions. Using JDBC transaction management mechanism is to use java.sql.connection object to complete transaction submission and rollback.

Core API encapsulation

Platform transaction manager: platform transaction manager. Spring manages transactions. When the transaction manager must be used for transaction configuration, the core method is to obtain transactions, commit transactions, and roll back transactions.

Transactiondefinition: this object encapsulates transaction details (transaction definition, transaction attributes), such as isolation level, read-only, timeout, etc.

Transactionstatus: used to record the current transaction running status. For example, whether there is a savepoint and whether the transaction is completed. The spring bottom layer performs corresponding operations according to the state.

8. Configuration file

In the spring configuration file, the following core contents are usually configured;

In the actual development, the complex project configuration is very complicated and difficult to manage. There may be dozens of configuration files involving different environments in the project, which are simplified in a unified way in the springboot framework.

9. Environment integration SSM, SSH

The spring framework has strong integration capabilities, such as the common integration of mybatis, MVC, hibernate, redis and other components, which provides great convenience for the integration of the development environment. The overall responsibilities are divided into several layers: control layer, business logic layer, data persistence layer, domain module layer and middleware layer, so as to help developers build clear structure and good reusability in a short time Easy to maintain web applications.

10. Design pattern

Singleton mode: the management of bean objects in the spring framework. The default singleton mode can also be explicitly identified as multi instance mode.

Factory mode: generate class objects through corresponding factories. This design method conforms to the "open close" principle. Usage of beanfactory and bean in spring framework.

Adapter mode: in spring MVC execution control, the front controller dispatcherservlet calls the processor adapter to execute the handler, the processor adapter executes the handler, and returns modelandview to the adapter.

Responsibility chain pattern: the core method of dispatcher servlet is dodispatch. The handlerexecutionchain only maintains a collection of handlerinterceptors, which can register corresponding interceptors. It does not directly process requests. It assigns requests to registered processors on the responsibility chain for execution, reducing the coupling between the responsibility chain itself and the processing logic.

2、 Springmvc pattern

1. MVC mode concept

Spring MVC is a request driven lightweight web framework based on MVC design pattern implemented in Java. It comes from the spring framework and is seamlessly integrated with the spring framework. It uses the idea of MVC architecture pattern to decouple the responsibilities of the web layer. The structure is loose, and almost all kinds of views can be used in spring MVC. Each module is separated, the coupling degree is very low, and it is easy to expand. Seamless integration with spring, simple, flexible and easy to use.

2. Execution process

Initiate a request to the front-end controller dispatcherservlet; The front-end controller requests the handler mapping to search, and the handler can search according to the XML configuration and annotation;

Processor mapper handlermapping returns a handler to the front end controller; The front-end controller calls the processor adapter to execute the handler; The processor adapter executes the handler;

The handler returns modelandview to the adapter after execution; The processor adapter returns modelandview to the front controller. Modelandview is an underlying object of spring MVC framework, including model and view;

The front-end controller requests the view parser to parse the view and parse it into a real view according to the logical view name; The view parser returns view to the front controller; The front-end controller performs view rendering, which fills the model data (in the modelandview object) into the request field; The front-end controller responds the result to the user;

3. Core components

Front end controller: after the request leaves the browser, the first one to arrive is the dispatcher servlet, which is the center of the whole process control.

Processor mapper: route to the specified interface according to the requested URL, and the user requests to find the handler processor.

Processor adapter: it executes handler according to specific rules and supports multiple processors. The processing methods in various processors are different.

Processor: it processes user requests, involves specific business logic, and needs to be developed according to business requirements.

View parser: generates a view from the response result of the request, and resolves it into a physical view name according to the logical view name, which is the specific page address.

View: the MVC framework provides support for many view types, including JSP, freemaker, PDF, etc.

4. Parameter processing

Requestparam: it is mainly used to obtain parameters in the control layer of the spring MVC framework. There are three common parameters: DefaultValue indicates setting the default value, required sets whether it is a parameter that must be passed in through the Boolean, and value indicates the name of the passed in parameter.

Requestbody: for receiving the JSON string data passed to the back end in the request body, the get method has no request body, so when using @ requestbody to receive data, you can't submit data in the get method, but you need to submit it in the post method.

ResponseBody: this annotation is used for the return object of the method. You can configure the converter to specify the data response format. If the data you want to return is not the view page, but the specified data format, such as JSON, XML, etc.

5. Integrate spring framework

6. Compare Webflux

Responsive programming is a declarative programming paradigm based on data flow and change transmission. Webflux is an integral part of responsive programming at the web control end. It is explained on the spring official website. It is not to replace springmvc, but to provide solutions for more scenarios.

3、 Springboot framework

1. Common basic functions

2. Consolidate data sources

3. Integrate common middleware

The whole springboot framework is based on the specification of many conventions on the spring framework. The underlying principle has not changed. It is more familiar with various usages and can be understood when used more.

4、 Comparative analysis

The spring framework is the lowest implementation principle relative to the spring open source ecosystem. Springmvc is based on it and mainly simplifies the development of the web control layer. For example, the previous struts and servlets have been gradually replaced.

On the basis of Spring + MVC, springboot implements a powerful convention configuration, integrates the complex environment and simplifies the development configuration. Business development is the same. In the SSM environment, regardless of the project configuration, startup and debugging are very complex and continue to be simplified at the springboot level. Therefore, after understanding the Convention configuration specification, springboot learns to, It's basically easy to get started.

5、 Source code address

GitHub·地址
https://github.com/cicadasmile
GitEE·地址
https://gitee.com/cicadasmile

Recommended reading: programming system sorting

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>