Spring consolidates mybatis,2 data sources and uses DYNAMICDATASOURCE+AOP to switch the data source based on the annotations on the method before the method call.
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs
I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xs i:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd "> <context:property-placeholder location=" classpath:db.properties "/> <!--<bean id=" Captchaproduce R "class=" Com.google.code.kaptcha.impl.DefaultKaptcha ">--> <!--<property name= "config" >--> <!--<bean class= "Com.google.code.kaptcha.util.Config" >--&
Gt <!--<constructor-arg>--> <!--<props>--> <!--<pro P key= "Kaptcha.border" >yes</prop>--> <!--<prop key= "Kaptcha.border.color" >105 ,179,90</prop>--> <!--<prop key= "Kaptcha.textproducer.font.color" >blue</prop&
gt;--> <!--<prop key= "Kaptcha.image.width" >250</prop>--> <!--<prop key= "Kaptcha.image.height" >90</prop>--> <!--<prop key= "KAPTC Ha.textproducer.font.size ">80</prop>--> <!--<prop key=" Kaptcha.session.key ">c ode</prop>--> <!--<prop key= "Kaptcha.textproducer.char.length" >4</prop>--> <!--<prop key= "kaptcha.textproducer.font.names" > Yue à 綋, 妤 bee 綋, Atlas , 呴 din Daeng </PROP>--&G
T
<!--</props>--> <!--</constructor-arg>--> <!--</bean>--> <!--</property>--> <!--</bean>--> <bean id= "Basicdatasource" class= "org.apache.co Mmons.dbcp2.BasicDataSource "> <property name=" url "value=" ${dbcp2.url} "/> <property name=" Driv
Erclassname "value=" ${dbcp2.driverclassname} "/> <property name=" username "value=" ${dbcp2.username} "/> <property name= "Password" value= "${dbcp2.password}"/> <property name= "initialsize" value= "${dbcp2.in Itialsize} "/> <property name=" Timebetweenevictionrunsmillis "value=" ${dbcp2.timebetweenevictionrunsmillis} " /> <property name= "poolpreparedstatements" value= "${dbcp2.poolpreparedstatements}"/> <property NAme= "maxopenpreparedstatements" value= "${dbcp2.maxopenpreparedstatements}"/> <property name= "RemoveAbandone Dtimeout "value=" ${dbcp2.removeabandonedtimeout} "/> <property name=" Testonborrow "value=" ${dbcp2.testOnBorro W} "/> <property name=" Testonreturn "value=" ${dbcp2.testonreturn} "/> <property name=" TestWhileI Dle "value=" ${dbcp2.testwhileidle} "/> </bean> <bean id=" BasicDataSource2 "class=" org.apache.commons.db Cp2. Basicdatasource "> <property name=" url "value=" ${dbcp2.url} "/> <property name=" Driverclassname " Value= "${dbcp2.driverclassname}"/> <property name= "username" value= "${dbcp2.username}"/> <pro Perty name= "Password" value= "${dbcp2.password}"/> <property name= "initialsize" value= "${dbcp2.initialsize}"/
> <property name= "timebetweenevictionrunsmillis" value= "${dbcp2.timebetweenevictionrunsmillis}"/> <property name= "pOolpreparedstatements "value=" ${dbcp2.poolpreparedstatements} "/> <property name=" MaxOpenPreparedStatements " Value= "${dbcp2.maxopenpreparedstatements}"/> <property name= "removeabandonedtimeout" value= "${dbcp2.removeA Bandonedtimeout} "/> <property name=" Testonborrow "value=" ${dbcp2.testonborrow} "/> <property na Me= "Testonreturn" value= "${dbcp2.testonreturn}"/> <property name= "Testwhileidle" value= "${dbcp2.testWhileIdl
e} "/> </bean> <bean id=" sqlsessionfactory "class=" Org.mybatis.spring.SqlSessionFactoryBean "> <property name= "DataSource" ref= "DataSource"/> </bean> <bean name= "Scannerconfigurer" class= "or G.mybatis.spring.mapper.mapperscannerconfigurer "> <property name=" sqlsessionfactorybeanname "value=" sqlSess Ionfactory "/> <property name=" basepackage "value=" Com.lutai.admin.*.dao "/> </bean> <bea n id= "SqltransactionmanageR "class=" Org.springframework.jdbc.datasource.DataSourceTransactionManager "> <property name=" DataSource "ref = "DataSource"/> </bean> <!--<! – Raccoon apply annotation Yue hydrogen 箟 Bang example Å –>--> <!--<aop:aspectj-autoproxy expose-proxy= "true"/>--> < !--<bean id= "test" class= "Com.zheyue.authserver.controller.TestBean" ></bean>--> <!--<bean id= "A" class= "Com.zheyue.authserver.controller.AspectTest" ></bean>--> <tx:annotation-driven Transaction-manager= "Sqltransactionmanager" order= "2"/> <bean id= "UserService" class= " Com.zheyue.authserver.service.impl.UserServiceImpl "></bean> <!--configuring multi-data source mapping relationships-<bean id=" data
Source "class=" Com.zheyue.authserver.datasource.DynamicDataSource "> <property name=" targetdatasources "> <map key-type= "java.lang.String" > <entry key= "DataSource1" value-ref= "BasicdatasouRce "></entry> <entry key=" DataSource2 "value-ref=" BasicDataSource2 "></entry> </map> </property> <!--default destination data source is your main library data source-<property name= "defaulttargetd Atasource "ref=" Basicdatasource "/> </bean> <bean id=" Exchange class= "Com.zheyue.authserver.datasource . Datasourceexchange "></bean> <aop:config expose-proxy=" true "> <aop:pointcut id=" pointcut "exp ression= "Execution (* *.save* (..))" /> <aop:advisor advice-ref= "Exchange" pointcut-ref= "Pointcut" order= "1"/> </aop:config> </be Ans>Custom annotations
Package Com.zheyue.authserver.datasource;
Import java.lang.annotation.*;
/**
* Created by FD on 2016/10/18.
*/
@Target ({elementtype.method, elementtype.type})
@Retention (retentionpolicy.runtime)
@Documented Public
@interface DataSource {
String name () default datasource.master;
public static String master = "Basicdatasource";
public static String slave = "BasicDataSource2";
}
AOP interception
Package Com.zheyue.authserver.datasource;
Import Org.aopalliance.intercept.MethodInterceptor;
Import org.aopalliance.intercept.MethodInvocation;
/**
* Created by FD on 2016/10/18.
*
/public class Datasourceexchange implements Methodinterceptor {public
Object invoke (methodinvocation Invocation) throws Throwable {
System.out.println (Invocation.getmethod (). Getannotation (Datasource.class));
System.out.println (Invocation.getmethod (). Getdeclaredannotation (Datasource.class));
String datasourcename = Invocation.getmethod (). Getannotation (Datasource.class). Name ();
Datasourceholder.setdatasource (datasourcename);
SYSTEM.OUT.PRINTLN ("Use data source" +datasourcename); try {
// invocation.proceed (); } catch (Exception e) {
// e.printstacktrace (); } finally {
//// }
invocation.proceed ();
return null;
}
}
Threadlocal
Package Com.zheyue.authserver.datasource;
/**
* Created by FD on 2016/10/18.
*/Public
class Datasourceholder {public
static final threadlocal<string> datasources = new threadlocal& Lt String> ();
public static void Setdatasource (String datasourcename) {
datasources.set (datasourcename);
}
public static String Getdatasource () {
return (String) datasources.get ();
}
public static void Clear () {
datasources.remove ();
}
}
Dynamic Data sources
Package Com.zheyue.authserver.datasource;
Import Org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* Created by FD on 2016/10/18.
*/Public
class Dynamicdatasource extends Abstractroutingdatasource {
@Override
protected Object Determinecurrentlookupkey () {
return Datasourceholder.getdatasource ();
}
}
Business
Package Com.zheyue.authserver.service.impl;
Import Com.zheyue.authserver.datasource.DataSource;
Import Com.zheyue.authserver.service.UserService;
Import Org.springframework.aop.framework.AopContext;
Import org.springframework.transaction.annotation.Propagation;
Import org.springframework.transaction.annotation.Transactional;
/**
* Created by FD on 2016/10/18.
*
/public class Userserviceimpl implements UserService {public
void Save () {
System.out.println (" Start execution of the Sava () method ");
try {
UserService userservice = (userservice) aopcontext.currentproxy ();
Userservice.save2 ();
} catch (Exception e) {
e.printstacktrace ();
}
finally {
}
} public
void Save2 () {
System.out.println ("Start execution Sava2 () method");
}
}
Package com.zheyue.authserver.service;