In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to achieve transaction nesting processing in spring, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The principle of spring transaction is dynamic proxy. When the transaction is nested, only the first method executed has the transaction, and the second nested method will be executed as an ordinary method, so we need to get the current proxy class again and generate the transaction again by the proxy. The specific processing methods are as follows: 1. Write a utility class to get the current service.
@ Componentpublic class SpringUtil implements ApplicationContextAware {private static ApplicationContext applicationContext; @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {if (SpringUtil.applicationContext = = null) {SpringUtil.applicationContext = applicationContext;} System.out.println ("= ApplicationContext configuration is successful. In a normal class, you can get the applicationContext object by calling SpringUtils.getAppContext (), applicationContext=" + SpringUtil.applicationContext+ "="). System.out.println ("-");} / / get applicationContext public static ApplicationContext getApplicationContext () {return applicationContext;} / / get Bean through name. Public static Object getBean (String name) {return getApplicationContext (). GetBean (name);} / / get Bean through class. Public static T getBean (Class clazz) {return getApplicationContext () .getBean (clazz);} / / return the specified Bean public static T getBean (String name,Class clazz) {return getApplicationContext () .getBean (name, clazz) through name and Clazz;}}
2. Get the current proxy class, convert it to the current service, and then call the nested method.
((EntityService) AopContext.currentProxy ()) .updateById (entity)
If your code reports an error after this, please add this comment to the class
@ EnableAspectJAutoProxy (proxyTargetClass = true, exposeProxy = true)
The above is how to achieve transaction nesting processing in spring. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.