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

How to use spring aop to achieve mysql read-write separation in business layer

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article focuses on "how to use spring aop to achieve business layer mysql read-write separation", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to use spring aop to achieve business layer mysql read-write separation"!

1. Use spring aop interception mechanism to realize the dynamic selection of data sources.

The import java.lang.annotation.ElementType; import java.lang.annotation.Target; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; / * RUNTIME * compiler will record the comments in the class file, and VM will retain the comments at run time, so it can be read reflectively. * @ author yangGuang * / @ Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.METHOD) public @ interface DataSource {String value ();}

3. Using Spring's AbstractRoutingDataSource to solve the problem of multiple data sources reference: http://blog.csdn.net/alaahong/article/details/8707915

Import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class ChooseDataSource extends AbstractRoutingDataSource {@ Override protected Object determineCurrentLookupKey () {return HandleDataSource.getDataSource ();}}

4. Using ThreadLocal to solve Thread Safety problems

Public class HandleDataSource {public static final ThreadLocal holder = new ThreadLocal (); public static void putDataSource (String datasource) {holder.set (datasource);} public static String getDataSource () {return holder.get ();}}

5. Define a data source aspect class, accessed through aop, configured in the spring configuration file, so no aop annotations are used.

Import java.lang.reflect.Method; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; / / @ Aspect / / @ Component public class DataSourceAspect {/ / @ Pointcut ("execution (* com.apc.cms.service.*.* (..)") Public void pointCut () {}; / / @ Before (value = "pointCut ()") public void before (JoinPoint point) {Object target = point.getTarget (); System.out.println (target.toString ()); String method = point.getSignature () .getName (); System.out.println (method) Class [] classz = target.getClass (). GetInterfaces (); Class [] parameterTypes = ((MethodSignature) point.getSignature ()) .getMethod () .getParameterTypes (); try {Method m = classz [0] .getMethod (method, parameterTypes); System.out.println (m.getName ()) If (m! = null & & m.isAnnotationPresent (DataSource.class)) {DataSource data = m.getAnnotation (DataSource.class); HandleDataSource.putDataSource (data.value ());} catch (Exception e) {e.printStackTrace () }}}

6. Configure applicationContext.xml

7. Using annotations, dynamically select the data source, read and write the library respectively.

DataSource ("write") public void update (User user) {userMapper.update (user);} @ DataSource ("read") public Document getDocById (long id) {return documentMapper.getById (id);}

Test write operation: you can modify the data and master database data through the application, and find that the data of the slave database has been updated synchronously, so the defined write operations are all written to the library.

Test read operation: modify the slave database data in the background, check that the data of the master database has not been modified, refresh in the application page, and find that the slave database data is read, indicating the separation of ok between read and write.

Summary of problems encountered:

Question 1: the project is a maven project, which uses the Spring aop mechanism. In addition to the core jar package of spring, the jar package that needs to be used is aspectj.jar,aspectjweaver.jar,aopalliance.jar to view the pom in the project and find that there is a lack of dependency packages.

Since these jar are not available in the local warehouse, find the maven central repository that can provide downloading jar packages, configure them to maven, and update them automatically:

Nexus nexus http://repository.sonatype.org/content/groups/public/ default

The configuration project depends on the jar, mainly because of the lack of these two.

Aspectj aspectjrt 1.5.4 aspectj aspectjweaver 1.5.4 so far, I believe you have a deeper understanding of "how to use spring aop to achieve business layer mysql read and write separation". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Servers

Wechat

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

12
Report