This article source: making here | | GitEE, click here

First, the Spring Framework

1. Framework overview

Spring is an open source framework, and one of the main advantages of the framework is its layered architecture, which allows users to choose which components to use, while providing an integrated framework for J2EE application development. Spring uses basic JavaBeans to do things that were previously only possible with EJBs. Spring is a layered, lightweight open source framework.

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

2. Core components

Core Container: Contains Bean creation, configuration, management, and other functions.

AOP facet programming: Can help decouple applications.

Data access: integrated JDBC, commonly used DAO layer framework Hibernate, MyBatis, etc.

Web programming: MVC framework for integrating processes to achieve interface logic and application separation.

3. Bean object understanding

The Spring container is responsible for creating, assembling, setting properties, and thus managing the entire life cycle of objects, called Bean objects.

Assembly mode: XML format, annotation scanning, Java code assembly.

Scope: Used to determine the number of instances of beans created by Spring, such as singleton beans and prototype beans. Singleton default singleton, prototype multi-instance, request request, session session level, global-session.

Lifecycle: instantiation, attribute loading, pre – and post-initialization management, destruction.

4, commonly used core annotations

Controller: Mark a class as Handler, based on @Mapping annotations (@GetMapping, @PostMapping, @PutMapping, @DeleteMapping) to associate the Mapping between the request and the Controller method. The Controller can then be accessed by request.

RequestMapping: An annotation that handles the mapping of requested addresses, which can be applied to a class or method. For a class, all methods in the class that respond to requests have an address annotated on the class as the parent path.

Resource: according to the ByName automatic injection, you need to import the package javax.mail. The annotation. The Resource. The @Resource annotation has two important attributes: name and type, and Spring resolves the @Resource annotation’s name attribute to the bean’s name, and the type attribute to the bean’s type.

Service: Replaces the Bean management of a specific configuration file. Beans defined are singletons by default, and the default name is the class name with a lowercase initial.

5. IOC and DI thought

The IOC container

The object coupling relationship in the Java system is very complex, which is the reason that the system modules depend on each other and the micro-service modules call each other and request each other. It is one of the core problems of software engineering to reduce the coupling degree between system modules, between objects and between services of micro-services. This is because the core idea of the Spring framework is IoC Inversion of Control, which is used to decouple objects.

Dependency injection

The action of IOC to establish a relationship with an object is called DI Dependency Injection. Dependency: If object A needs to use the functions of object B, object A is said to depend on object B. Injection: Instantiate object B within object A to use the functionality of object B. This action is called injection.

6. AOP facet programming

A technique for achieving unified maintenance of program functions through precompilation and runtime dynamic proxies. Core role: It can isolate the various parts of the business logic, so as to reduce the coupling degree between the various parts of the business logic, and improve the reusability and development efficiency of the program. AOP provides a new scheme to replace inheritance and delegation, and it is more concise and clear to use. AOP is a hot concept in software development.

Implementation: JDK dynamic proxy, CGlib bytecode enhancement, Spring semi-automatic proxy, Spring fully automatic proxy.

7. Transaction management

A transaction is a series of operations (SQL statements) that are executed as a single logical unit of work. Either all or none of these operations were successful. The essence of Spring transaction management is to encapsulate the operations supported by the database for transactions. Using the JDBC transaction management mechanism, it is to use java.sql.Connection object to complete the commit and rollback of transactions.

Core API encapsulation

PlatformTransactionManager: the platform transaction manager, Spring management affairs, must use the configuration of the transaction manager for business, core method: get transaction, commit the transaction, and roll back the transaction.

TransactionDefinition: This object encapsulates transaction details (TransactionDefinition, transaction attributes), such as isolation level, read-only, timeout, etc.

TransactionStatus: Used to record the current TransactionStatus. For example: whether there is a savepoint, whether the transaction is complete. The Spring underlying layer acts accordingly based on the state.

8. Configuration files

In a Spring configuration file, the following core content is typically configured;

  • Read external configuration files, such as JDBC parameters;
  • Configure database connection pooling, such as Druid, C3P0, etc.
  • Integrate environment configurations, such as SSM or SSH integration;
  • The controls that govern Transaction transactions
  • Integrate common components such as mail, tasks, MQ, etc.

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

9. Environment integration of SSM and SSH

Spring framework has strong integration ability, such as the common integration of MyBatis, MVC, Hibernate, Redis and other components, which provides great convenience for the integration of development environment. The overall responsibility is divided into several layers: The control layer, the business logic layer, the data persistence layer, the domain module layer and the middleware layer can help the developers to build the Web application program with clear structure, good reusability and easy maintenance in a short time.

10. Design patterns

Singleton pattern: The management of Bean objects in the Spring framework, which is the default singleton, can also be explicitly identified as multi-instance pattern.

Factory pattern: Generates the objects of the class through the corresponding factory, which is designed in accordance with the “on closed” principle. Use of BeanFactory and Beans in the Spring Framework.

Adapter pattern: In SpringMVC execution control, the front-end controller DispatcherServlet calls the processor adapter to execute Handler, and the processor adapter to execute Handler returns ModelandView to the adapter.

Chain of Responsibility pattern: The core DispatcherServlet method doDispatch. The HandlerExecutionChain just maintains the set of HandlerInterceptor, and can register corresponding interceptors in it. It does not process requests directly itself, but allocates the requests to registered processors in the responsibility chain for execution, reducing the coupling degree between the responsibility chain itself and the processing logic.

Second, Spring MVC pattern

1. MVC pattern concept

SpringMVC is a lightweight Web framework based on the Java implementation of the MVC design pattern. SpringMVC is a lightweight Web framework based on the Java implementation of the MVC design pattern. SpringMVC is seamlessly integrated with the Spring Framework, using the idea of the MVC architectural pattern to decouple the Web layer responsibilities. Loosely structured, almost all kinds of views can be used in SpringMVC, the modules are separated and very poorly coupled, and easy to extend. Seamless integration with Spring is simple, flexible and easy to use.

2. Execute the process

Initiate a request to the front-end controller DispatcherServlet; The front-end controller requests HandlerMapping to search, and Handler can search according to XML configuration and annotations.

HandlerMapping returns a Handler to the front-end controller; The front controller calls the processor adapter to execute the Handler; The processor adapter to perform the Handler;

The Handler returns ModelAndView to the adapter when it finishes executing. Processor adapter controller returns ModelAndView forward end, ModelAndView is an underlying object for springmvc framework, including the Model and the view;

The front-end controller asks the view parser to parse the view to the actual view based on the logical view name. The View parser returns the View to the front-end controller; The front controller renders the view, which populates 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: When a request leaves the browser, the first thing that arrives is the DispatcherServlet, which is the center of the process control.

Processor mapper: Routed to the specified interface based on the requested URL, the user requests to find the Handler Handler.

Processor adapters: Execute handlers according to specific rules and support multiple processors, with different processing methods in each processor.

Processor: handles user requests, involving specific business logic, which needs to be developed according to business requirements.

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

Views: The MVC framework provides support for many View types, including: JSP, Freemarker, PDF, etc.

4. Parameter processing

RequestParam: is used to retrieve parameters in the control layer of the Spring MVC framework. The three common parameters are defaultValue, required, which sets the defaultValue, required, which sets whether or not the parameter must be passed in, and value, which indicates the name of the parameter passed in.

RequestBody: The RequestBody receives the JSON string data that is passed to the back end of the RequestBody. The GET method has no RequestBody. Therefore, when receiving the data using @RequestBody, you cannot submit the data using GET method, you need to submit the data using POST method.

ResponseBody: This annotation is used for the return object of the method, which can be configured by the converter to specify the data response format. If you want to return data in a format other than the View attempt page, you can use it when you specify the data format, such as JSON, XML, etc.

5. Integrate the Spring Framework

  • Configure scan interface file;
  • How to enable MVC’s default annotation mapping;
  • Configure the view parser;
  • The web.xml configuration loads the Spring-MVC file;

6. Compare WebFlux

Responsive programming is a declarative programming paradigm based on data flow and change transfer. WebFlux is an component of responsive programming on the Web control side, which is explained on the Spring official website. It is not intended to replace Spring MVC, but to provide a solution for more scenarios.

SpringBoot Framework

1. Common basic functions

  • Environment building and annotation startup mechanism, log printing;
  • Global exception handling, timing task asynchronous task use;
  • Interceptor configuration, AOP facet programming, file management;
  • Integrate JWT, Shiro, Security and other common Security components;
  • Integrate the monitoring components of the Actuator, and the system is packaged and operated;

2. Integrate data sources

  • Integrate JDBCTemplate,JPA, multi-data source configuration;
  • Integrate Druid, C3P0 common connection pool;
  • Integrate MyBatis framework, integrate paging management;

3. Integrate common middleware

  • Integrated Redis Cache,Cache annotation mode;
  • Integrated ElasticSearch framework to achieve high performance search engine
  • Based on Swagger2, the interface management interface is constructed.

The entire SpringBoot framework is a specification based on many conventions on the Spring framework. The underlying principles have not changed. It is more about getting familiar with various usages and understanding them as you use them more often.

Fourth, comparative analysis

Spring framework is the lowest implementation principle relative to Spring open source ecology, and Spring MVC is based on it, which mainly simplifies the development of Web control layer. For example, the previous Struts and Servlet are gradually replaced.

SpringBoot, on the basis of Spring+ MVC, implements very powerful convention configuration and makes convention integration for complex environment to simplify development configuration. Business development is the same. Under SSM environment, startup and debugging of project configuration are very complex, but it is simplified continuously after SpringBoot level. So SpringBoot learning is pretty easy to get started with once you understand the convention configuration specification.

Five, the source code address

Making address GitEE, https://github.com/cicadasmile, https://gitee.com/cicadasmile

Recommended reading: Programming system reorganization

The serial number The project name Making the address GitEE address Recommend index
01 Java describes design patterns, algorithms, and data structures Making, click here GitEE, click here Being fostered fostered fostered fostered
02 Java Foundation, Concurrency, Object-oriented, Web Development Making, click here GitEE, click here Being fostered fostered fostered
03 SpringCloud microservice base component case detail Making, click here GitEE, click here Do do do
04 SpringCloud microservice architecture practical comprehensive case Making, click here GitEE, click here Being fostered fostered fostered fostered
05 Getting started with the SpringBoot Framework basics Making, click here GitEE, click here Being fostered fostered fostered
06 SpringBoot framework integrates common middleware development Making, click here GitEE, click here Being fostered fostered fostered fostered
07 Basic cases of data management, distribution, architecture design Making, click here GitEE, click here Being fostered fostered fostered fostered
08 Big data series, storage, components, computing and other frameworks Making, click here GitEE, click here Being fostered fostered fostered fostered