spring aop 多数据源

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 添加夹包 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捐助),没钱捧个人场,谢谢各位。



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

 

 

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
监控 Java 开发者
Spring AOP动态代理
Spring AOP动态代理
40 1
|
1月前
|
Java Spring 容器
Spring的AOP失效场景详解
Spring的AOP失效场景详解
87 0
|
2月前
|
XML Java 编译器
Spring AOP初步理解及使用
Spring AOP初步理解及使用
49 0
|
2月前
|
Java Spring
[Spring]aop的配置与使用
[Spring]aop的配置与使用
40 0
[Spring]aop的配置与使用
|
22天前
|
设计模式 Java Maven
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
Spring Aop 底层责任链思路实现-springaopdi-ceng-ze-ren-lian-si-lu-shi-xian
31 1
|
2月前
|
XML Java 数据格式
5个点轻松搞定Spring AOP底层实现原理
AOP 也是 Spring 中一个较为重要的内容,相对于传统的 OOP 模式,AOP 有很多让人难以理解的地方,本篇文章将向大家介绍 AOP 的实现方法及其底层实现,内容包括:
44 1
|
15天前
|
XML Java Maven
Spring之Aop的注解使用
Spring之Aop的注解使用
|
21天前
|
Java Spring
Spring 如何实现 AOP
Spring 如何实现 AOP
17 0
|
30天前
|
Java 编译器 程序员
Spring AOP 和 AspectJ 的比较
Spring AOP 和 AspectJ 的比较
34 0
|
30天前
|
Java 数据库连接 数据库
Spring Boot整合MyBatis Plus集成多数据源轻松实现数据读写分离
Spring Boot整合MyBatis Plus集成多数据源轻松实现数据读写分离
26 2