Java topic notes ~ use AspectJ's AOP configuration management transaction (master)

The disadvantage of using XML to configure the transaction proxy is that each target class needs to configure the transaction proxy. When there are many target classes, the configuration file will become very bloated.

Using the XML configuration advisor approach can automatically generate a transaction proxy for each class that matches the pointcut expression. Its usage is very simple, just delete the configuration of the transaction agent in the previous code, and then replace it with the following content.

Step1: Copy the project

Duplicate the trans_sale project and rename it trans_sal_aop. Modified on this basis.

The overall project structure is as follows:

Step2: maven depends on pom.xml

Newly added aspectj dependency coordinates

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.3.26</version>
</dependency>

 

 

Step3: Add a transaction manager to the container

Step4: Configure transaction notification

Set related properties for transaction notifications. Used to specify how transactions are to be woven into which methods. For example, transaction requirements applied to the buy method are required, and the business needs to be rolled back when an exception occurs in the buy method.

 

Step5: Configure Enhancer

Specify to whom to weave the configured transaction notification.

 

 

Step6: Modify the test class

What the test class gets from the container is the target object.

Run the program:

 Check out the database:

 

Guess you like

Origin blog.csdn.net/qq_53142796/article/details/132173257