Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of Spring transaction annotation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What is the principle of Spring transaction annotation? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

When using Spring, we often see annotations like annotation-driven in the configuration file, which means to support annotations, which can be understood bluntly according to the prefixes tx, mvc and so on.

Are those that support transaction annotations (@ Transactional).

Is to support mvc annotations (various annotations in Controller that can be used with MVC).

There is an attribute that specifies which transaction manager to use, such as:

/ / txManager is the specified transaction manager

This goes on layer by layer, so it's easy to understand that we can use the @ Transactionl annotation to control the transaction.

Another thing to mention is that Spring uses aop to manipulate Java bytecode through asm to manage the transaction before and after the method.

So can we use @ Transactional annotations on all classes managed by spring in the program? it is certain that @ Transactional annotations can be used on Service, so there are always some people who want to know whether they can be used in Controller? The answer is obviously "not necessarily" (related to the time configuration), which is explained below:

There is a passage on the spring-framework-reference.pdf document:

Only looks for @ Transactional on beans in the same application contextit is defined in. This means that, if you put in a WebApplicationContextfor a DispatcherServlet, it only checks for @ Transactional beans in your controllers,and not your services.

Only the @ Transactional annotation on the bean defined in the same application context is found. If you put it in the Dispatcher application context, it only checks the @ Transactional comment on the controller (Controller), not the @ Transactional comment on your services.

So, it's certain that we can use transaction annotations on Controller, but we don't recommend it, just to illustrate the use of spring pairs.

This is the answer to the question about the principle of annotation in Spring transactions. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report