What is the Spring Framework? What are the main modules of the Spring Framework?

Author: Guide Columbia
link: https: //www.zhihu.com/question/48427693/answer/692943779
Source: know almost
copyrighted by the author. For commercial reproduction, please contact the author for authorization, and for non-commercial reproduction, please indicate the source.

 

We generally say that the Spring framework refers to the Spring Framework, which is a collection of many modules, these modules are: core container, data access / integration, Web, AOP (aspect-oriented programming), tools, messaging and test modules, as shown below As shown. The main purpose of Spring's framework is to simplify Java back-end development.

List some important Spring modules?

Some important Spring Framework modules are:

  • Spring Core: Basic, it can be said that all other Spring functions need to depend on this library. It mainly provides IOC dependency injection function.
  • ** Spring Aspects **: This module provides support for integration with AspectJ.
  • Spring AOP : Provides aspect-oriented programming implementation.
  • Spring JDBC : Java database connection.
  • Spring JMS : Java Message Service.
  • Spring ORM : Used to support ORM tools such as Hibernate.
  • Spring Web : Provide support for creating Web applications.
  • Spring Test : Provides support for JUnit and TestNG testing.

Talk about your understanding of Spring IoC and AOP

IoC

IoC (Inverse of Control: Inversion of Control) but a design idea is to create objects originally in the program manual control, handed over to the Spring framework to manage. IoC is also used in other languages ​​and is not unique to Spirng. The IoC container is the carrier used by Spring to implement IoC. The IoC container is actually a Map (key, value), and various objects are stored in the Map.

The interdependence between objects is handed over to the IOC container for management, and the IOC container completes the injection of objects. This can greatly simplify the development of applications and liberate applications from complex dependencies. The IOC container is like a factory. When we need to create an object, we only need to configure the configuration file / annotation, regardless of how the object is created. In the actual project, a Service class may have hundreds or even thousands of classes as its bottom layer. If we need to instantiate this Service, you may have to figure out the constructors of all the bottom classes of this Service every time. Drive people crazy. If you use IOC, you only need to configure it, and then refer to where it is needed, which greatly increases the maintainability of the project and reduces the difficulty of development.

In the Spring era, we generally configured Beans through XML files. Later, developers felt that the configuration of XML files was not very good, so the SpringBoot annotation configuration slowly became popular.

Recommended reading:

Spring IOC initialization process:

IOC source code reading

AOP

AOP (Aspect-Oriented Programming: aspect-oriented programming) can encapsulate logic or responsibilities (such as transaction processing, log management, permission control, etc.) that are irrelevant to the business but are commonly called by the business module , which is convenient to reduce the repeated code of the system , To reduce the coupling between modules , and is conducive to future scalability and maintainability .

Spring AOP is based on a dynamic proxy . If the object to be proxied implements an interface, then Spring AOP will use JDK Proxy to create a proxy object. For objects that do not implement an interface, you cannot use JDK Proxy to proxy. At this time, Spring AOP will use Cglib . At this time, Spring AOP will use Cglib to generate a subclass of the proxy object as a proxy, as shown in the following figure:

Of course, you can also use AspectJ, Spring AOP has integrated AspectJ, AspectJ should be regarded as the most complete AOP framework in the Java ecosystem.

After using AOP, we can abstract some common functions and use them directly where needed, which greatly simplifies the amount of code. It is also convenient when we need to add new functions, which also improves the system scalability. AOP is used in scenarios such as logging, transaction management, and so on.

What is the difference between Spring AOP and AspectJ AOP?

Spring AOP is a runtime enhancement, while AspectJ is a compile-time enhancement. Spring AOP is based on Proxying, while AspectJ is based on Bytecode Manipulation.

Spring AOP has integrated AspectJ, AspectJ should be regarded as the most complete AOP framework in the Java ecosystem. AspectJ is more powerful than Spring AOP, but Spring AOP is relatively simpler,

If we have fewer cross-sections, then the performance difference between the two is not significant. However, when there are too many aspects, it is better to choose AspectJ, which is much faster than Spring AOP.

What are the scope of beans in Spring?

  • singleton: The only bean instance. The beans in Spring are singletons by default.
  • prototype: Each request creates a new bean instance.
  • request: Each HTTP request will generate a new bean, which is only valid within the current HTTP request.
  • session: Each HTTP request will generate a new bean, which is only valid in the current HTTP session.
  • global-session: The global session scope is only meaningful in portlet-based web applications. Spring 5 is gone. Portlets are small Java Web plug-ins that can generate semantic code (eg HTML) fragments. They are based on portlet containers and can handle HTTP requests like servlets. However, unlike servlets, each portlet has a different session

Bean life cycle in Spring?

What are the advantages of Spring MVC?

Does SpringMVC work?

The principle is shown below:

A small typo in the picture above: The entry function of Spring MVC, which is the front-end controller DispatcherServlet, is used to receive requests and respond to results.

Process description (important):

  1. The client (browser) sends a request and requests it directly to DispatcherServlet.
  2. DispatcherServlet calls HandlerMapping according to the request information to parse the Handler corresponding to the request.
  3. After parsing to the corresponding Handler (that is, the Controller we usually say), it is processed by the HandlerAdapter adapter.
  4. The HandlerAdapter will call the real processor to process the request according to the Handler and process the corresponding business logic.
  5. After the processor processes the business, it returns a ModelAndView object, Model is the returned data object, and View is a logical View.
  6. ViewResolver will find the actual View based on the logical View.
  7. DispaterServlet passes the returned Model to the View (view rendering).
  8. Return View to the requester (browser)

What design patterns are used in the Spring framework?

A few more common ones:

  • Factory design pattern : Spring uses the factory pattern to pass BeanFactoryand ApplicationContextcreate bean objects.
  • Agent design pattern : the realization of Spring AOP function.
  • Singleton design pattern : Beans in Spring are singletons by default.
  • Template method pattern : Spring, jdbcTemplateand hibernateTemplateother classes that end with Template operation on the database, they use the template pattern.
  • Wrapper design pattern : Our project needs to connect to multiple databases, and different customers will access different databases as needed in each visit. This model allows us to dynamically switch between different data sources based on customer needs.
  • Observer pattern: The Spring event-driven model is a classic application of the Observer pattern.
  • Adapter pattern : Spring AOP enhancements or notifications (Advice) use the adapter pattern, and spring MVC also uses the adapter pattern adaptation Controller.

How many ways does Spring manage transactions?

  1. Programmatic transactions, hard-coded in the code. (Not recommended)
  2. Declarative transactions, configured in the configuration file (recommended)

There are two types of declarative transactions:

  1. XML-based declarative transactions
  2. Annotation-based declarative transactions

What are the isolation levels in Spring transactions?

Five constants representing the isolation level are defined in the TransactionDefinition interface:

  • TransactionDefinition.ISOLATION_DEFAULT: Use the default isolation level of the back-end database, Mysql default REPEATABLE_READ isolation level Oracle default READ_COMMITTED isolation level.
  • TransactionDefinition.ISOLATION_READ_UNCOMMITTED: the lowest isolation level, allowing reading of uncommitted data changes, which may result in dirty reads, phantom reads, or non-repeatable reads
  • TransactionDefinition.ISOLATION_READ_COMMITTED: allows reading of data that has been submitted by concurrent transactions, can prevent dirty reads, but magic reads or non-repeatable reads may still occur
  • TransactionDefinition.ISOLATION_REPEATABLE_READ: The results of multiple reads on the same field are consistent, unless the data is modified by its own transaction, which can prevent dirty reads and non-repeatable reads, but magic reads may still occur.
  • TransactionDefinition.ISOLATION_SERIALIZABLE: The highest isolation level, fully compliant with the ACID isolation level. All transactions are executed one by one in turn, so that there is no possibility of interference between transactions, that is, this level can prevent dirty reads, non-repeatable reads, and magic reads . But this will seriously affect the performance of the program. Normally, this level is not used.

What kinds of transaction propagation behaviors in Spring transactions?

Support the current transaction:

  • TransactionDefinition.PROPAGATION_REQUIRED: If there is currently a transaction, join the transaction; if there is no transaction, create a new transaction.
  • TransactionDefinition.PROPAGATION_SUPPORTS: If there is currently a transaction, then join the transaction; if there is currently no transaction, then continue to run in a non-transactional way.
  • TransactionDefinition.PROPAGATION_MANDATORY: If there is currently a transaction, then join the transaction; if there is currently no transaction, throw an exception. (Mandatory: mandatory)

The case that does not support the current transaction:

  • TransactionDefinition.PROPAGATION_REQUIRES_NEW: Create a new transaction, if there is currently a transaction, then suspend the current transaction.
  • TransactionDefinition.PROPAGATION_NOT_SUPPORTED: Run in non-transactional mode, if there is currently a transaction, then suspend the current transaction.
  • TransactionDefinition.PROPAGATION_NEVER: Run in non-transactional mode, if a transaction currently exists, an exception is thrown.

Other situations:

  • TransactionDefinition.PROPAGATION_NESTED: If there is currently a transaction, create a transaction to run as a nested transaction of the current transaction; if there is no transaction currently, the value is equivalent to TransactionDefinition.PROPAGATION_REQUIRED.

Guess you like

Origin www.cnblogs.com/fangdie/p/12700937.html