Spring aop application for database read/write splitting springaop

Source: Internet
Author: User

Spring aop application for database read/write splitting springaop

Spring and Mybatis implement master-slave read/write separation for MySQL databases. The principle is that multiple data sources are configured, and one set is configured for the corresponding sqlsessionfactory, transactionmanager, and transaction proxy, if there are multiple slave databases or databases, more and more information needs to be configured, which is far from elegant enough. In our programming world, there is a rule: conventions are better than configurations. Therefore, Sping aop is used to implement a simple database separation solution. The specific implementation code is put on Github. The address is as follows:

Https://github.com/bridgeli/practical-util/tree/master/src/main/java/cn/bridgeli

If you want to use the simple method, you can download the code and put it in your own project. Of course, the more elegant method is to pack it into a jar package and put it in the project, as for the specific jar method, the old man will not talk about it here. I believe that readers who read this article will certainly do it. Of course, this code will not take effect automatically. Since Spring Aop is used to implement database read/write splitting,
So it will certainly involve the configuration of Aop, so there is the following configuration in the spring-mybatis.xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: context = "http://www.springframework.org/schema/context" xmlns: p = "http://www.springframework.org/schema/p" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http: // Www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <! -- Configure the write data source --> <bean id = "masterDataSource" class = "com. alibaba. druid. pool. druidDataSource "destroy-method =" close "> <property name =" driverClassName "value =" $ {bridgeli. jdbc. driver} "/> <property name =" url "value =" $ {bridgeli. jdbc. url} "/> <property name =" username "value =" $ {bridgeli. jdbc. username} "/> <property name =" password "value =" $ {bridgeli. jdbc. password} "/> <property name =" validationQuery "value =" $ {Bridgeli. jdbc. validationQuery} "/> <property name =" initialSize "value =" 1 "/> <property name =" maxActive "value =" 20 "/> <property name =" minIdle" value = "0"/> <property name = "maxWait" value = "60000"/> <property name = "testOnBorrow" value = "false"/> <property name =" testOnReturn "value =" false "/> <property name =" testWhileIdle "value =" true "/> <property name =" timeBetweenEvictionRunsMillis "value =" 60000" /> <Property name = "minEvictableIdleTimeMillis" value = "25200000"/> <property name = "removeAbandoned" value = "true"/> <property name = "removeAbandonedTimeout" value =" 1800 "/> <property name =" logAbandoned "value =" true "/> <property name =" filters "value =" stat "/> </bean> <! -- Configure the read data source --> <bean id = "parentSlaveDataSource" class = "com. alibaba. druid. pool. druidDataSource "destroy-method =" close "> <property name =" driverClassName "value =" $ {bridgeli. jdbc. driver} "/> <property name =" validationQuery "value =" $ {bridgeli. jdbc. validationQuery} "/> <property name =" initialSize "value =" 1 "/> <property name =" maxActive "value =" 20 "/> <property name =" minIdle" value = "0"/> <property name = "m AxWait "value =" 60000 "/> <property name =" testOnBorrow "value =" false "/> <property name =" testOnReturn "value =" false "/> <property name = "testWhileIdle" value = "true"/> <property name = "timeBetweenEvictionRunsMillis" value = "60000"/> <property name = "minEvictableIdleTimeMillis" value = "25200000"/> <property name = "removeAbandoned" value = "true"/> <property name = "removeAbandonedTimeout" value = "1800"/> <Property name = "logAbandoned" value = "true"/> <property name = "filters" value = "stat"/> </bean> <bean id = "slaveDataSource1" class = "com. alibaba. druid. pool. druidDataSource "destroy-method =" close "parent =" parentSlaveDataSource "> <property name =" url "value =" $ {bridgeli_slave1.jdbc.url} "/> <property name =" username "value = "$ {bridgeli_slave1.jdbc.username}"/> <property name = "password" value = "$ {bridgeli_sl Ave1.jdbc. password} "/> </bean> <bean id =" dataSource "class =" cn. bridgeli. datasource. masterSlaveDataSource "> <property name =" targetDataSources "> <map> <entry key-ref =" masterDataSource "value-ref =" masterDataSource "/> <entry key-ref =" slaveDataSource1 "value-ref =" slaveDataSource1 "/> </map> </property> <property name =" defaultTargetDataSource "ref =" masterDataSource "/> <property name =" masterSlaveSelector "Ref =" dataSelector "/> </bean> <bean id =" dataSelector "class =" cn. bridgeli. datasource. masterSlaveSelectorByPoll "> <property name =" masters "> <list> <ref bean =" masterDataSource "/> </list> </property> <property name =" slaves "> <list> <ref bean = "slaveperformance1"/> </list> </property> <property name = "defaultDataSource" ref = "masterDataSource"/> </bean> <aop: aspectj-autoproxy/> <! -- Mybaits data factory --> <bean id = "sqlSessionFactory" class = "org. mybatis. spring. sqlSessionFactoryBean "> <property name =" dataSource "ref =" dataSource "/> </bean> <! -- Automatically scans all annotation paths --> <bean class = "org. mybatis. spring. mapper. mapperScannerConfigurer "> <property name =" basePackage "value =" cn. bridgeli. mapper "/> <! -- <Property name = "sqlSessionFactory" ref = "sqlSessionFactory"/> --> <property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactory"> </property> </bean> <! -- Database aspect --> <bean id = "masterSlaveAspect" class = "cn. bridgeli. datasource. masterSlaveAspect "> <property name =" prefixMasters "> <list> <value> save </value> <value> update </value> <value> delete </value> </ list> </property> </bean> <aop: config> <aop: aspect id = "c" ref = "masterSlaveAspect"> <aop: pointcut id = "tx" expression = "execution (* cn. bridgeli. service .. *. *(..)) "/> <aop: before pointcut-ref =" tx "method =" before "/> </aop: aspect> </aop: config> <context: annotation-config/> <context: component-scan base-package = "cn. bridgeli "/> </beans>

 

In this way, we use Spring Aop to implement read and write separation of the database. When reading, we use the slaveDataSource1 data source, and when writing, we use the masterDataSource data source.
Ah, wait, where is the Convention better than the configuration? How do they know which methods are used to read databases and write databases? You can read this configuration file carefully and you will find that a MasterSlaveAspect is configured in Row 3,
That is to say, the service layer in the Code (why is it the service layer ?) The Methods configured here are headers and will all go through the write database. So when we want a method to go through the master database, you must add the method prefix or use the existing prefix,
This requires that we have to agree to take the method of the master database as the header, that is, the Convention is better than the configuration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.