spring aop 多数据源

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: 添加夹包 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捐助),没钱捧个人场,谢谢各位。



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

 

 

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
Java
Spring5入门到实战------9、AOP基本概念、底层原理、JDK动态代理实现
这篇文章是Spring5框架的实战教程,深入讲解了AOP的基本概念、如何利用动态代理实现AOP,特别是通过JDK动态代理机制在不修改源代码的情况下为业务逻辑添加新功能,降低代码耦合度,并通过具体代码示例演示了JDK动态代理的实现过程。
Spring5入门到实战------9、AOP基本概念、底层原理、JDK动态代理实现
|
7天前
|
设计模式 Java 测试技术
spring复习04,静态代理动态代理,AOP
这篇文章讲解了Java代理模式的相关知识,包括静态代理和动态代理(JDK动态代理和CGLIB),以及AOP(面向切面编程)的概念和在Spring框架中的应用。文章还提供了详细的示例代码,演示了如何使用Spring AOP进行方法增强和代理对象的创建。
spring复习04,静态代理动态代理,AOP
|
20天前
|
Java 数据库连接 数据库
Spring基础3——AOP,事务管理
AOP简介、入门案例、工作流程、切入点表达式、环绕通知、通知获取参数或返回值或异常、事务管理
Spring基础3——AOP,事务管理
|
2月前
|
XML Java 数据格式
Spring5入门到实战------11、使用XML方式实现AOP切面编程。具体代码+讲解
这篇文章是Spring5框架的AOP切面编程教程,通过XML配置方式,详细讲解了如何创建被增强类和增强类,如何在Spring配置文件中定义切入点和切面,以及如何将增强逻辑应用到具体方法上。文章通过具体的代码示例和测试结果,展示了使用XML配置实现AOP的过程,并强调了虽然注解开发更为便捷,但掌握XML配置也是非常重要的。
Spring5入门到实战------11、使用XML方式实现AOP切面编程。具体代码+讲解
|
2月前
|
缓存 Java 开发者
Spring高手之路22——AOP切面类的封装与解析
本篇文章深入解析了Spring AOP的工作机制,包括Advisor和TargetSource的构建与作用。通过详尽的源码分析和实际案例,帮助开发者全面理解AOP的核心技术,提升在实际项目中的应用能力。
23 0
Spring高手之路22——AOP切面类的封装与解析
|
2月前
|
Java Spring
Spring的AOP组件详解
该文章主要介绍了Spring AOP(面向切面编程)组件的实现原理,包括Spring AOP的基础概念、动态代理模式、AOP组件的实现以及Spring选择JDK动态代理或CGLIB动态代理的依据。
Spring的AOP组件详解
|
2月前
|
Java Spring XML
掌握面向切面编程的秘密武器:Spring AOP 让你的代码优雅转身,横切关注点再也不是难题!
【8月更文挑战第31天】面向切面编程(AOP)通过切面封装横切关注点,如日志记录、事务管理等,使业务逻辑更清晰。Spring AOP提供强大工具,无需在业务代码中硬编码这些功能。本文将深入探讨Spring AOP的概念、工作原理及实际应用,展示如何通过基于注解的配置创建切面,优化代码结构并提高可维护性。通过示例说明如何定义切面类、通知方法及其应用时机,实现方法调用前后的日志记录,展示AOP在分离关注点和添加新功能方面的优势。
38 0
|
2月前
|
缓存 安全 Java
Spring AOP 中两种代理类型的限制
【8月更文挑战第22天】
16 0
|
2月前
|
Java Spring
|
2月前
|
XML Java 数据库
Spring5入门到实战------10、操作术语解释--Aspectj注解开发实例。AOP切面编程的实际应用
这篇文章是Spring5框架的实战教程,详细解释了AOP的关键术语,包括连接点、切入点、通知、切面,并展示了如何使用AspectJ注解来开发AOP实例,包括切入点表达式的编写、增强方法的配置、代理对象的创建和优先级设置,以及如何通过注解方式实现完全的AOP配置。
下一篇
无影云桌面