`
knight_black_bob
  • 浏览: 823736 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring aop 多数据源

阅读更多

添加夹包 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捐助),没钱捧个人场,谢谢各位。



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

 

 

  • 大小: 8.8 KB
0
1
分享到:
评论
2 楼 knight_black_bob 2015-11-02  
jinxiuwei 写道
能把工程发我一份么,jinxiuwei.1988@163.com,学习一下谢谢

http://download.csdn.net/download/knight_black_bob/9233289
1 楼 jinxiuwei 2015-10-29  
能把工程发我一份么,jinxiuwei.1988@163.com,学习一下谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics