开发者社区> curiousby> 正文

spring aop 多数据源

简介: 添加夹包 c3p0-0.9.5.jar  mchange-commons-java-0.2.8.jar   结构:   根绝结构 不同 注入数据源   db-config.properties one.
+关注继续查看

添加夹包 c3p0-0.9.5.jar  mchange-commons-java-0.2.8.jar

 

结构:



 

根绝结构 不同 注入数据源

 

db-config.properties

one.jdbc.driverClass=com.mysql.jdbc.Driver
one.jdbc.url=jdbc:mysql://localhost:3306/fusionweb?useUnicode=true&characterEncoding=utf-8
one.jdbc.user=root
one.jdbc.password=root
one.jdbc.initialPoolSize=5
one.jdbc.minPoolSize=5
one.jdbc.maxPoolSize=20
one.jdbc.checkoutTimeout=20000
one.jdbc.idleConnectionTestPeriod=120
one.jdbc.maxIdleTime=60
one.jdbc.maxStatements=100
one.jdbc.testConnectionOnCheckout=false


two.jdbc.driverClass=com.mysql.jdbc.Driver
two.jdbc.url=jdbc:mysql://localhost:3306/self_help?useUnicode=true&characterEncoding=utf-8
two.jdbc.user=root
two.jdbc.password=root
two.jdbc.initialPoolSize=5
two.jdbc.minPoolSize=5
two.jdbc.maxPoolSize=20
two.jdbc.checkoutTimeout=20000
two.jdbc.idleConnectionTestPeriod=120
two.jdbc.maxIdleTime=60
two.jdbc.maxStatements=100
two.jdbc.testConnectionOnCheckout=false


three.jdbc.driverClass=com.mysql.jdbc.Driver
three.jdbc.url=jdbc:mysql://localhost:3306/fusion_prob?useUnicode=true&characterEncoding=utf-8
three.jdbc.user=root
three.jdbc.password=root
three.jdbc.initialPoolSize=5
three.jdbc.minPoolSize=5
three.jdbc.maxPoolSize=20
three.jdbc.checkoutTimeout=20000
three.jdbc.idleConnectionTestPeriod=120
three.jdbc.maxIdleTime=60
three.jdbc.maxStatements=100
three.jdbc.testConnectionOnCheckout=false



 

 

 

<?xml version="1.0" encoding="UTF-8"?>

<beans  
	xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"  
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"  
    xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"  
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd    
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd    
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd    
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd    
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd    
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd    
    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd    
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd    
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd    
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd    
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd    
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd    
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"
	>
  
	 <context:annotation-config />
  <aop:aspectj-autoproxy />
  <context:component-scan base-package="com">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	 
	 
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                <value>classpath:db-config.properties</value>  
            </list>  
        </property>  
    </bean>  
  
    <bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
        destroy-method="close">  
       <property name="driverClass" value="${one.jdbc.driverClass}" />
		<property name="jdbcUrl" value="${one.jdbc.url}" />
		<property name="user" value="${one.jdbc.user}" />
		<property name="password" value="${one.jdbc.password}" />
		<property name="initialPoolSize" value="${one.jdbc.initialPoolSize}" />
		<property name="minPoolSize" value="${one.jdbc.minPoolSize}" />
		<property name="maxPoolSize" value="${one.jdbc.maxPoolSize}" />
		<property name="checkoutTimeout" value="${one.jdbc.checkoutTimeout}" />
		<property name="idleConnectionTestPeriod" value="${one.jdbc.idleConnectionTestPeriod}" />
		<property name="maxIdleTime" value="${one.jdbc.maxIdleTime}" />
		<property name="maxStatements" value="${one.jdbc.maxStatements}" />
		<property name="testConnectionOnCheckout" value="${one.jdbc.testConnectionOnCheckout}" /> 
    </bean>  
  
    <bean id="dataSourceTwo" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
        destroy-method="close">  
        <property name="driverClass" value="${two.jdbc.driverClass}" />
		<property name="jdbcUrl" value="${two.jdbc.url}" />
		<property name="user" value="${two.jdbc.user}" /> 	
		<property name="password" value="${two.jdbc.password}" />
		<property name="initialPoolSize" value="${two.jdbc.initialPoolSize}" />
		<property name="minPoolSize" value="${two.jdbc.minPoolSize}" />
		<property name="maxPoolSize" value="${two.jdbc.maxPoolSize}" />
		<property name="checkoutTimeout" value="${two.jdbc.checkoutTimeout}" />
		<property name="idleConnectionTestPeriod" value="${two.jdbc.idleConnectionTestPeriod}" />
		<property name="maxIdleTime" value="${two.jdbc.maxIdleTime}" />
		<property name="maxStatements" value="${two.jdbc.maxStatements}" />
		<property name="testConnectionOnCheckout" value="${two.jdbc.testConnectionOnCheckout}" /> 
    </bean> 
    
     <bean id="dataSourceThree" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
        destroy-method="close">  
       <property name="driverClass" value="${three.jdbc.driverClass}" />
		<property name="jdbcUrl" value="${three.jdbc.url}" />
		<property name="user" value="${three.jdbc.user}" />
		<property name="password" value="${three.jdbc.password}" />
		<property name="initialPoolSize" value="${three.jdbc.initialPoolSize}" />
		<property name="minPoolSize" value="${three.jdbc.minPoolSize}" />
		<property name="maxPoolSize" value="${three.jdbc.maxPoolSize}" />
		<property name="checkoutTimeout" value="${three.jdbc.checkoutTimeout}" />
		<property name="idleConnectionTestPeriod" value="${three.jdbc.idleConnectionTestPeriod}" />
		<property name="maxIdleTime" value="${three.jdbc.maxIdleTime}" />
		<property name="maxStatements" value="${three.jdbc.maxStatements}" />
		<property name="testConnectionOnCheckout" value="${three.jdbc.testConnectionOnCheckout}" /> 
    </bean>   
  
   
  
    
  <bean id="dataSourceInterceptor" class="com.commons.dynamictasource.DataSourceInterceptor" />
  
    <aop:config>  
        <aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor">  
            <aop:pointcut id="daoOne" expression="execution(* com.onweb.*.*(..))" />  
            <aop:pointcut id="daoTwo" expression="execution(* com.twoweb.*.*(..))" />  
            <aop:pointcut id="daoThree" expression="execution(* com.threeweb.*.*(..))" />  
            <aop:before pointcut-ref="daoOne" method="setdataSourceOne" />  
            <aop:before pointcut-ref="daoTwo" method="setdataSourceTwo" />  
             <aop:before pointcut-ref="daoThree" method="setdataSourceThree" />  
        </aop:aspect>  
    </aop:config>  
     
     
      <bean id="dataSource" class="com.commons.dynamictasource.DynamicDataSource">  
        <property name="targetDataSources">  
            <map key-type="java.lang.String">  
                <entry value-ref="dataSourceOne" key="dataSourceOne"></entry>  
                <entry value-ref="dataSourceTwo" key="dataSourceTwo"></entry>  
                <entry value-ref="dataSourceThree" key="dataSourceThree"></entry>  
            </map>  
        </property>  
        <property name="defaultTargetDataSource" ref="dataSourceOne">  
        </property>  
    </bean>  
</beans>   

 

package com.commons.dynamictasource;

public class DatabaseContextHolder {

	private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();  
	  
    public static void setCustomerType(String customerType) { 
        contextHolder.set(customerType);  
    }  
  
    public static String getCustomerType() {  
        return contextHolder.get();  
    }  
  
    public static void clearCustomerType() {  
        contextHolder.remove();  
    }  
	    
}

 

package com.commons.dynamictasource;

import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;

@Component  
public class DataSourceInterceptor {  
  
    public void setdataSourceOne(JoinPoint jp) {  
        DatabaseContextHolder.setCustomerType("dataSourceOne");  
    }  
      
    public void setdataSourceTwo(JoinPoint jp) {  
        DatabaseContextHolder.setCustomerType("dataSourceTwo");  
    }  
    
    public void setdataSourceThree(JoinPoint jp) {  
        DatabaseContextHolder.setCustomerType("dataSourceThree");  
    } 
}  

 

package com.commons.dynamictasource;

import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
 

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
 

public class DynamicDataSource  extends AbstractRoutingDataSource{  
  
	  
    @Override  
    protected Object determineCurrentLookupKey() {   
        return DatabaseContextHolder.getCustomerType();   
    }

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException { 
		return null;
	}

	 
  
}  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Spring Boot2.x-10 基于Spring Boot 2.1.2 + Mybatis 2.0.0实现多数据源,支持事务
Spring Boot2.x-10 基于Spring Boot 2.1.2 + Mybatis 2.0.0实现多数据源,支持事务
16 0
Spring Boot 整合多数据源,这才叫优雅~
Spring Boot 整合多数据源,这才叫优雅~
28 0
Spring Boot 与 多数据源那点事儿~
Spring Boot 与 多数据源那点事儿~
13 0
Spring boot +Mybatis多数据源的配置与使用
Spring boot +Mybatis多数据源的配置与使用
106 0
Spring 多数据源配置详解
前言 数据源是 JDBC 规范中用来获取关系型数据库连接的一个接口,主要通过池化技术来复用连接。
230 0
一文读懂Spring动态配置多数据源---源码详细分析(下)
一文读懂Spring动态配置多数据源---源码详细分析
836 0
一文读懂Spring动态配置多数据源---源码详细分析 (上)
一文读懂Spring动态配置多数据源---源码详细分析
646 0
3种方式实现多数据源控制/切换、实现读写分离;演示借助AbstractRoutingDataSource实现多数据源的动态切换代码【享学Spring】(下)
3种方式实现多数据源控制/切换、实现读写分离;演示借助AbstractRoutingDataSource实现多数据源的动态切换代码【享学Spring】(下)
84 0
3种方式实现多数据源控制/切换、实现读写分离;演示借助AbstractRoutingDataSource实现多数据源的动态切换代码【享学Spring】(中)
3种方式实现多数据源控制/切换、实现读写分离;演示借助AbstractRoutingDataSource实现多数据源的动态切换代码【享学Spring】(中)
164 0
3种方式实现多数据源控制/切换、实现读写分离;演示借助AbstractRoutingDataSource实现多数据源的动态切换代码【享学Spring】(上)
3种方式实现多数据源控制/切换、实现读写分离;演示借助AbstractRoutingDataSource实现多数据源的动态切换代码【享学Spring】(上)
181 0
+关注
curiousby
echo
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
相关实验场景
更多