[SpringBoot Series 3] SpringBoot uses transactions and AOP

Foreword:

Because SpringBoot is too simple to operate both, I will write them together.

Body (transaction):

1  /** 
2       * The use of transaction
 3       * in springboot is really super convenient, it is ok to add annotations directly, and even the configuration is saved
 4       * @return 
5       */ 
6      @Transactional
 7      public  int insertDemo() {
 8          
9          int flag = td.insertDemo("bo", "123456" );
 10          flag = td.insertDemo("bo", "123456" );
 11          return flag;
 12      }

it is more than words. The xml file does not need to be configured, the pom file does not need to be configured, and nothing needs to be configured. Adding the @Transactional annotation directly is super convenient. As for the propagation behavior of transactions, I will not talk about the isolation level here. You can read my other articles.

 

Body (AOP):

Spring's two major features, IoC and AOP, show that the status of AOP plays a pivotal role in Spring.

First of all, it depends on spring-boot-starter-aop, and secondly, there is no more, you can use it directly.

 1 @Aspect
 2 @Component
 3 public class HttpAspect {
 4     
 5     private final static Logger 
 6         logger = LoggerFactory.getLogger(HttpAspect.class);
 7     
 8     @Pointcut(value = "execution (public * com.LoginApplication.testMybatis(..))")
 9     public void log() {
10     }
11     
12     @Before(value = "log()")
13     public void doBefore() {
14         
15         logger.info("AOP pre-interception" );
 16  //         System.out.println("AOP pre-interception"); 
17      }
 18      
19      @After(value = "log()" )
 20      public  void doAfter() {
 21          
22          logger.info("AOP post interception" );
 23  //         System.out.println(""); 
24      }
 25 }

5 and 6 are the log prints of slf4j, you don't need to look at them. Lines 8 and 9 are to avoid repetition, and a template-like thing is built, and the value in it is a facet expression.

The first * means that any value can be returned, com is the package name, LoginApplication is the class name, and testMybatis is the method name, in which the method name can be replaced by * , which means that all of them are replaced. The .. in parentheses means that it accepts any type of parameter and any number of parameters.

For details, see this article: https://www.cnblogs.com/songshuiyang/p/7857515.html

The result is that every time the testMybatis method is called, the contents of before and after will be printed before and after it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325238400&siteId=291194637