Explain in detail the voice of spring learning and explicit transaction management
preface
In the previous section, we learned about the concept of transaction and the importance of transaction management, and used spring's programmatic transaction management through programming to deepen the learning of the importance of transaction management. However, because the use of programmatic transaction management is not very convenient, it is basically not used in daily development, In the following content, we will learn to use spring's declarative transaction management. One thing to understand here is that the implementation of spring's declarative transaction management is actually implemented through AOP, that is, create proxy objects for the original transaction management objects, so as to enhance transaction management
Transaction management configuration based on transactionproxyfactory bean
After the previous study, we can know that there are three ways to configure AOP in spring, namely, creating agents through proxyfactorybean, XML and annotation. Since spring transaction management is implemented through AOP, there are three different ways. First, let's look at the management method based on transactionproxyfactorybean
The first is the spring configuration file
Corresponding persistence layer code
Business layer code
After the above configuration, when using accountservice, spring will automatically supervise the transaction because the obtained object is the object after the proxy. What we need to do is to configure the corresponding transaction propagation type and transaction management level. This method obviously does not invade the code and, However, using this method means that there is no need to create corresponding proxy objects for different service objects, which is actually inconvenient. Next, let's look at the configuration method using AOP / TX namespace.
Transaction management configuration based on AOP / TX namespace
Since the above business operations are subject to transaction management, and we are basically familiar with the business after learning from the previous section, we will directly demonstrate the configured code here
It can be seen that transaction management can be more flexible through XML configuration
Annotation based transaction management configuration
The annotation based configuration method provides a simpler configuration method. You only need to annotate with the @ transactional annotation and start the corresponding scan.
The spring configuration file is also relatively simple
It can be seen that the annotation configuration method is the simplest configuration method, which is also used frequently in daily development
summary
This section mainly studies the configuration of spring declarative transaction management, including the use of transactionproxyfactory bean, XML configuration through AOP / TX namespace and annotation based configuration. Among them, annotation based configuration is relatively simple and frequently used
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.