Spring2衔接多数据库ITeye - 牛牛娱乐

Spring2衔接多数据库ITeye

2019-01-13 14:13:03 | 作者: 浩波 | 标签: | 浏览: 1876

public class DynamicDataSourceHolder { public static final ThreadLocal String holder = new ThreadLocal String public static void putDataSource(String name) { holder.set(name); public static String getDataSouce() { return holder.get(); }

 

public class DataSourceAspect {
 public void before(JoinPoint point)
 Object target = point.getTarget();
 String method = point.getSignature().getName();
 Class ? [] classz = target.getClass().getInterfaces();
 Class ? [] parameterTypes = ((MethodSignature) point.getSignature())
 .getMethod().getParameterTypes();
 try {
 Method m = classz[0].getMethod(method, parameterTypes);
 if (m != null m.isAnnotationPresent(DataSource.class)) {
 DataSource data = m
 .getAnnotation(DataSource.class);
 DynamicDataSourceHolder.putDataSource(data.value());
 System.out.println(data.value());
 } catch (Exception e) {
 // TODO: handle exception
}

 

 bean id="masterdataSource"
 property name="driverClassName" value="com.mysql.jdbc.Driver" / 
 property name="url" value="jdbc:mysql://127.0.0.1:3306/shop" / 
 property name="username" value="root" / 
 property name="password" value="yangyanping0615" / 
 /bean 
 bean id="slavedataSource"
 property name="driverClassName" value="com.mysql.jdbc.Driver" / 
 property name="url" value="jdbc:mysql://127.0.0.1:3306/test" / 
 property name="username" value="root" / 
 property name="password" value="yangyanping0615" / 
 /bean 
 beans:bean id="dataSource" 
 property name="targetDataSources" 
 map key-type="java.lang.String" 
 !-- write -- 
 entry key="master" value-ref="masterdataSource"/ 
 !-- read -- 
 entry key="slave" value-ref="slavedataSource"/ 
 /map 
 /property 
 property name="defaultTargetDataSource" ref="masterdataSource"/ 
 /beans:bean 
 bean id="transactionManager"
 property name="dataSource" ref="dataSource" / 
 /bean                          
			
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表牛牛娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章

阅读排行

  • 1
  • 2

    UTLITeye

    数据,目录,文件
  • 3
  • 4
  • 5

    java与函数式编程ITeye

    函数,编程,一些
  • 6
  • 7
  • 8
  • 9
  • 10