A few minutes will take you to quickly understand the theoretical knowledge of the Spring framework!

1. Two core understandings of Spring

The two cores of Spring are IOC (Inversion of Control) and AOP (Aspect-Oriented Programming).

IOC Inversion of Control: In fact, the right to manage objects is handed over to Spring. In the past, it was necessary to take the initiative to create objects and control the timing. Now this right is transferred to the Spring container, and the Spring container handles it according to the configuration file. Create instances and manage dependencies between instances, loose coupling between objects is also conducive to reuse functions. To put it bluntly, IOC allows the creation of objects without going to NEW, which can be automatically produced by Spring according to the configuration files we provide. When we need objects, we can directly obtain them from the container.

The bytecode location and information of the class are configured in the Spring configuration file. When the container is generated, the configuration file will be loaded to identify the bytecode information, and objects of the class will be created through reflection.

IOC injection methods: setter method, constructor, annotation method

AOP aspect-oriented programming: In fact, it encapsulates those public behaviors and logic extractions that have nothing to do with business but affect multiple objects into a reusable module. This module is actually an aspect. For example: log management, etc. Spring AOP uses dynamic proxy. Dynamic proxy means that the AOP framework does not modify the bytecode, but temporarily generates an AOP object for the method in memory every time it runs. This AOP object contains all the methods of the target object, and Enhanced processing is done at a specific cut point, and the method of the original object is called back.

2. Spring supports several scopes of beans

(1) singleton: the default is a singleton bean, and there is only one bean in a container

(2) Propotype: A bean is created every time an object is instantiated

(3) request: a request creates a bean, and the garbage collection container will recycle it after the request ends

(4) session: Similar to request, a session shares a bean, and the bean will be recycled after the session or session is closed

(5) global-session: Global scope, all sessions share an instance. If you want to declare a storage variable that is shared by all sessions, you can store the global variable in global-session.

3. The difference between BeanFactory and ApplicationContext

beanFactory is the highest interface of Spring. It implements some basic functions of the Spring container. It is very troublesome to call, and it is generally used for Spring itself. When the project starts, the bean will not be instantiated immediately, but will only be instantiated when it is called.

applicationContext is a sub-interface of beanFactory, which extends some functions. As soon as the project is started, it will load the bean and put it into the Spring container, and then get it when it is used.

4. Design patterns used by the Spring framework

(1) Factory mode: The beanFactory inside uses a simple factory mode for bean production.

(2) Singleton mode: The bean is a singleton by default, and there is only one bean in a container.

(3) Proxy mode: AOP in Spring is realized through JDK dynamic proxy and CGLIB bytecode technology.

(4) Observer method: define a one-to-many dependency of object keys. If a property of an object changes, other dependent objects can be notified and updated accordingly. The Listener in Spring is ApplicationListener.

(5) Template method: realize code reuse, such as JpaTemplate and RestTemplate.

5. Implementation method and principle of Spring transaction

The essence of Spring transactions is the database's support for transactions. If the database does not support transactions, Spring cannot implement transactions. The database implements data submission and rollback using binlog and relog.

The implementation methods are:

(1) Programmatic transactions: implementation of transaction management methods such as begintransactionManager(), commit(), and rollback().

(2) Declarative transaction: realized by @Transactional annotation.

6. Spring notification type and execution

(1) Pre-notification type: Executed before the pointcut runs.

(2) Post-notification type: Executed after the pointcut runs normally.

(3) Exception notification type: Executed when an exception occurs at the cut point.

(4) Final advice type: final execution at the pointcut.

(5) Surrounding notification type: Programmers can define the notification settings by themselves through coding to solve other notification timing problems.

7. Whether the Spring object defaults to a single instance or multiple instances, and whether the singleton bean has thread safety issues

Spring objects are singletons by default, but can be set to multiple instances.

The class corresponding to the singleton bean object may have mutable member variables. When there are threads in it to change the value of the member variables, multi-threads will operate the bean, which will cause thread safety problems. The reason for this is: if the multi-threaded operation changes the member variable, other threads cannot access the bean, causing data confusion.

You can avoid setting variable member variables in the bean object, define a ThreadLocal variable, and put all the variable member variables inside the variable.

8. The difference between @Resource and @Autowired dependency injection

@Resource It is an annotation provided by Spring. It is assembled by name by default. If no matching bean can be found by name, it will be assembled by type. If it cannot be found by name, an error will be reported. But you need to know that if the execution is specified through the name attribute, it will only be assembled according to the name. If it cannot be found, an error will be reported, and it will not be assembled according to the type class.

@Autowried It is provided natively by java. It is assembled according to the type by default. The dependent object must exist by default. If it is empty, you need to set required to false. If you want to implement assembly by name, you need to use the specified name together with @Qualifier.

9. Usage scenarios of @Qualifier

It must be used together with @Autowired, and cannot be used alone. The combination of the two realizes the assembly by name, which actually achieves the same function as @Resource.

10.Common annotations of Spring

@Component: can be used in various layers for object instantiation

@Controller: for the control layer, for object instantiation

@Serveice: for business layer, for object instantiation

@Repository: for dao layer, for object instantiation

@Value: dependency injection for simple properties

@Resource: To realize the assembly of objects, the default is to assemble according to the name, if not found, you can assemble according to the type

@Autowried: To realize the assembly of objects, the default is to assemble according to the type, if not found, an exception will be thrown

@Qualifier: Used together with @Autowried to achieve assembly by name

@Bean: mark on the method, tell the method to generate a bean object, and then hand it over to Spring for management and put it in the container

@ComponentScan: component scan

@Configiration: Having this annotation is considered a configuration class. Once the system starts, Spring will create the objects that need to be created in it, put them into the bean container, and get them from it when needed

@Transactional: placed on the method, with transaction management function

@Import: Introduce the content of another configuration class in the configuration class

@PostConstruct, @PreDestory: Used to set the operations that need to be performed after the Spring object is created and before it is destroyed

@PropertySource: used to introduce other properties configuration files

11. Spring's transaction propagation behavior

The propagation behavior of transactions actually refers to how Spring handles these transactions when multiple transactions exist at the same time.

(1) propagation_requried: If there is a current transaction, join the transaction, if there is no transaction, create a new transaction.

(2) propagation_support: supports the current transaction. If there is a transaction currently, join the transaction, if there is no transaction, execute as non-transaction.

(3) propagation_not_support: Executed as a non-transaction. If a transaction currently exists, the pending transaction is executed non-transactionally.

(4) propagation_never: Execute with non-transaction. An exception is thrown if a transaction exists.

(5) propagation_requries_new: Create a new transaction, regardless of whether there is a current transaction or not.

(6) propagation_mandatory: supports the current transaction, if there is a transaction, join the transaction, and if it does not exist, remove the exception.

(7) propagation_nested: If there is a current transaction, execute it in a nested transaction. If there is no transaction, execute according to the requried attribute.

12. Isolation level in Spring

(1) isolation_default: This is the default transaction isolation of PlatfromTransactionManager, using the default isolation level of the database.

(2) isolation_read_uncommitted: read uncommitted, allowing another transaction to see the uncommitted data of this transaction.

(3) isolation_read_committed: read committed, to ensure that the data modified by one transaction can be read by another transaction only after it is committed, and you can see the update of the existing records by the transaction. Solve the problem of dirty data.

(4) isolation_repeatable_read: repeatable read, to ensure that the data modified by one transaction can be read by another transaction only after committing, but the update of existing records by the transaction cannot be seen. row table.

(5) isolation_serializable: A transaction cannot see updates made to the database by other transactions during execution. Standard lock.

Guess you like

Origin blog.csdn.net/lf21qp/article/details/131401317