In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to achieve transaction control through AOP surround notification. I hope you will gain something after reading this article. Let's discuss it together.
Transaction control through AOP surround notification 1, import of related dependencies org.springframework spring-context 5.0.2.RELEASE c3p0 c3p0 0.9.1.2 org.aspectj aspectjweaver 1.8.7 2, configuration of connection pooling and opening of AOP annotations
The following is the xml configuration, of course, you can also use annotation-only configuration
2. A utility class that creates a link utility class package com.gzl.utils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import javax.sql.DataSource;import java.sql.Connection;/** * connection, which is used to obtain a connection from the data source and bind to the thread * / @ Component ("connectionUtils") public class ConnectionUtils {private ThreadLocal tl = new ThreadLocal (); @ Autowired private DataSource dataSource / * get the connection on the current thread * @ return * / public Connection getThreadConnection () {try {/ / 1. First get Connection conn = tl.get () from ThreadLocal; / / 2. Determine whether there is a connection if (conn = = null) {/ / 3 on the current thread. Get a connection from the data source and store it in ThreadLocal conn = dataSource.getConnection (); tl.set (conn);} / / 4. Returns the connection return conn;} catch (Exception e) {throw new RuntimeException (e);}} / * unbind the connection and thread on the current thread * / public void removeConnection () {tl.remove ();}} 3, AOP wraps the transaction class package com.gzl.utils;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.* Import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;/** * tool classes related to transaction management, which include opening transactions, committing transactions, rolling back transactions, and releasing connections * / @ Component ("txManager") @ Aspectpublic class TransactionManager {@ Autowired private ConnectionUtils connectionUtils / * classes or methods that require transaction control. EL expression configuration * / @ Pointcut ("execution (* com.gzl.service.impl.*.* (..)") Private void pt1 () {} / * * Open transaction * / public void beginTransaction () {try {connectionUtils.getThreadConnection () .setAutoCommit (false);} catch (Exception e) {e.printStackTrace () }} / * commit transaction * / public void commit () {try {connectionUtils.getThreadConnection () .commit ();} catch (Exception e) {e.printStackTrace () }} / * rollback transaction * / public void rollback () {try {connectionUtils.getThreadConnection () .rollback ();} catch (Exception e) {e.printStackTrace () }} / * release connection * / public void release () {try {connectionUtils.getThreadConnection () .close (); / / return connectionUtils.removeConnection () to the connection pool;} catch (Exception e) {e.printStackTrace () } @ Around ("pt1 ()") public Object aroundAdvice (ProceedingJoinPoint pjp) {Object rtValue = null; try {/ / 1. Get the parameter Object [] args = pjp.getArgs (); / / 2. Open transaction this.beginTransaction (); / / 3. Execute method rtValue = pjp.proceed (args); / / 4. Commit transaction this.commit (); / / return the result return rtValue;} catch (Throwable e) {/ / 5. Rollback transaction this.rollback (); throw new RuntimeException (e);} finally {/ / 6. The idea of releasing resource this.release ();} spring AOP surround notification
Surround Notification Around Advice is to execute relevant services before and after the specified program. The design idea is as follows:
1. Design an interface package com.spring.service;public interface IComponent {public void bussiness1 (); public void bussiness2 (); public void bussiness3 ();} 2. Write the implementation of this interface package com.spring.service;public class Component implements IComponent {@ Override public void bussiness1 () {/ / TODO Auto-generated method stub System.out.println ("this is Business 1");} @ Override public void bussiness2 () {/ / TODO Auto-generated method stub System.out.println ("this is Business 2") } @ Override public void bussiness3 () {/ / TODO Auto-generated method stub System.out.println ("this is Business 3");}} 3. Write the logic code of the pre-notification
The code must implement the org.aopalliance.intercept.Method Interceptor interface, and all the required services are written here.
4. Write XML configuration file
To implement the AOP surround notification through a proxy, take a look at the source code of the org.aopalliance.intercept.MethodInterceptor interface. This interface is not an interface within Spring, but is specified by the AOP Alliance standard, but Spring has a specific implementation process for this interface, and the interface integrates all AOP frameworks that comply with the AOP Alliance standard.
Surround notification is equivalent to the combination of pre-notification and post-notification, except that in the invoke () method of MethodInterceptor, you are free to use the proceed () method provided by MethodInvocation to execute the method of the target object, while the proceed () method will return the return result after the execution of the target method, which can be modified before the end of the invoke method.
Write a class that wraps the notification, which implements the MethodInterceptor interface. Here, the proceed () method of MethodInvocation is called, that is, methods such as business1 in the target object Component are called, and verification and notification execution are added before and after this method, respectively, and then modify the configuration file to remove the configuration of pre-notification and post-notification. You only need to add the surround notification. The specific code is as follows:
All you need to do here is to configure a Bean that surrounds the notification, and configure this Bean into the interceptorNames to do all the work, the test code is the same as before, and you can see that the result is the same as before.
After reading this article, I believe you have a certain understanding of "how to achieve transaction control through AOP surround notification". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.