springMVC多数据源使用 跨库跨连接

来源:互联网 发布:足球赌球什么软件 编辑:程序博客网 时间:2024/05/16 01:47

转载:http://blog.csdn.net/a973893384/article/details/21460121


有的时候项目中可能要从另外一个系统取数据  如果另外一个系统能提供接口就很好解决 如果没有接口 便可以配置多个数据源切换访问

<1>:这是数据源和事务扫描注入的配置 访问多个数据源只需要建立多个数据源和事务这一套配置文件

这是第一个数据源

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:jdbc="http://www.springframework.org/schema/jdbc"   
  6.     xmlns:jee="http://www.springframework.org/schema/jee"  
  7.     xmlns:tx="http://www.springframework.org/schema/tx"   
  8.     xmlns:aop="http://www.springframework.org/schema/aop"  
  9.     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  10.     xsi:schemaLocation="  
  11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  13.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd  
  14.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd  
  15.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
  16.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd  
  17.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
  18.         http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"  
  19.     default-lazy-init="true">  
  20.   
  21.     <description>Spring公共配置 </description>  
  22.       
  23.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  24.         <property name="locations">  
  25.             <list>  
  26.                 <value>classpath:resources.properties</value>  
  27.                 <!--  <value>classpath:memcached.properties</value> -->  
  28.             </list>  
  29.         </property>  
  30.     </bean>  
  31.       
  32.     <!-- dubbo配置 -->  
  33.     <!-- <dubbo:application name="xaUserRegPro" />  
  34.     <dubbo:registry address="multicast://" />  
  35.     <dubbo:reference id="userRegProService" interface="com.xinnet.xa.service.UserRegProService"  timeout="6000"/> -->  
  36.       
  37.  <!-- **************** druid 监控连接池配置 ***************** -->  
  38.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  39.           
  40.         <!-- 基本属性 url、user、password -->  
  41.         <property name="url" value="${url}" />  
  42.         <property name="username" value="${username}" />  
  43.         <property name="password" value="${password}" />  
  44.           
  45.         <!-- 配置初始化大小、最小、最大 -->  
  46.         <property name="initialSize" value="${initialSize}" />  
  47.         <property name="minIdle" value="${minIdle}" />  
  48.         <property name="maxActive" value="${maxActive}" />  
  49.           
  50.         <!-- 配置获取连接等待超时的时间 -->  
  51.         <property name="maxWait" value="${maxWait}" />  
  52.           
  53.         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
  54.         <property name="timeBetweenEvictionRunsMillis" value="60000" />  
  55.           
  56.         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
  57.         <property name="minEvictableIdleTimeMillis" value="300000" />  
  58.         <property name="validationQuery" value="SELECT 'x'" />  
  59.         <property name="testWhileIdle" value="true" />  
  60.         <property name="testOnBorrow" value="false" />  
  61.         <property name="testOnReturn" value="false" />  
  62.           
  63.         <!-- 打开PSCache,并且指定每个连接上PSCache的大小  -->  
  64.         <!-- 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false -->  
  65.         <property name="poolPreparedStatements" value="${poolPreparedStatements}" />  
  66.         <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />  
  67.           
  68.         <!-- 对泄漏的连接 自动关闭 -->  
  69.         <property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 打开removeAbandoned功能 -->  
  70.         <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 1800秒,也就是30分钟 -->  
  71.         <property name="logAbandoned" value="${logAbandoned}" /> <!-- 关闭abanded连接时输出错误日志 -->  
  72.           
  73.         <!-- 配置监控统计拦截的filters -->  
  74.         <property name="filters" value="mergeStat" />  
  75.         <!--  <property name="filters" value="stat" /> -->  
  76.         <!-- 慢日志查询  缺省为3秒  修改为10秒 10000 -->  
  77.         <property name="connectionProperties" value="druid.stat.slowSqlMillis=5000" />  
  78.           
  79.         <!-- DruidDataSource各自独立 , 支持配置公用监控数据 -->  
  80.         <!-- <property name="useGloalDataSourceStat" value="true" /> -->  
  81.     </bean>  
  82.       
  83.     <!-- druid 监控  spring  -->  
  84.     <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"/>  
  85.     <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">  
  86.         <property name="patterns">  
  87.             <list>  
  88.                 <value>com.xinnet.*.service.*</value>  
  89.             </list>  
  90.         </property>  
  91.     </bean>  
  92.     <aop:config>  
  93.         <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />  
  94.     </aop:config>  
  95.       
  96.     <!-- MyBatis Mapper.XMl 配置 -->  
  97.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  98.         <property name="dataSource" ref="dataSource" />  
  99.         <property name="configLocation" value="classpath:config/mybatis.xml" />  
  100.         <property name="mapperLocations">  
  101.             <list>  
  102.             <!-- 自动匹配Mapper映射文件  -->  
  103.                 <value>classpath:mapper/**/*-mapper.xml</value>  
  104.             </list>  
  105.         </property>  
  106.         <!-- 添加插件 -->  
  107.         <property name="plugins">  
  108.             <array>  
  109.                 <ref bean="pagePlugin" />  
  110.             </array>  
  111.         </property>  
  112.     </bean>  
  113.       
  114.     <!-- 支持文件上传相关 -->  
  115.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>  
  116.       
  117.     <!-- 分页插件,根据方言自动添加分页信息,默认要求 -->  
  118.     <bean id="pagePlugin" class="com.xinnet.core.mybatis.plugin.PagePlugin">  
  119.         <property name="properties">  
  120.             <props>  
  121.                 <prop key="dialect">com.xinnet.core.mybatis.dialet.MySQLDialect</prop>  
  122.                 <prop key="pageSqlId">.*query.*</prop>  
  123.             </props>  
  124.         </property>  
  125.     </bean>  
  126.     <!-- redis客户端 -->  
  127.      <!-- jedis pool配置  -->    
  128.     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">    
  129.         <property name="maxActive" value="${redis.maxActive}" />    
  130.         <property name="maxIdle" value="${redis.maxIdle}" />    
  131.         <property name="maxWait" value="${redis.maxWait}" />    
  132.         <property name="testOnBorrow" value="${redis.testOnBorrow}" />    
  133.     </bean>    
  134.       
  135.     <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">    
  136.         <property name="usePool" value="true"></property>    
  137.         <property name="hostName" value="${redis.host}" />    
  138.         <property name="port" value="${redis.port}" />    
  139.         <property name="password" value="${redis.pass}" />    
  140.         <property name="timeout" value="${redis.timeout}" />    
  141.         <property name="database" value="${redis.default.db}"></property>    
  142.         <constructor-arg index="0" ref="jedisPoolConfig" />    
  143.     </bean>    
  144.     <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">    
  145.         <property name="connectionFactory" ref="jedisConnectionFactory" />    
  146.     </bean>  
  147.      
  148.     <!-- ***************事务配置************** -->  
  149.     <tx:annotation-driven transaction-manager="transactionManager"/>  
  150.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  151.         <property name="dataSource" ref="dataSource" />  
  152.     </bean>  
  153.     <aop:config>    
  154.              <aop:advisor pointcut="execution(* com.xinnet..service..*.*(..))"  advice-ref="txAdvice" />    
  155.     </aop:config>    
  156.     <tx:advice id="txAdvice" transaction-manager="transactionManager">    
  157.         <tx:attributes>    
  158.             <tx:method name="get*" read-only="true" />    
  159.             <tx:method name="query*" read-only="true" />    
  160.             <tx:method name="find*" read-only="true" />    
  161.             <tx:method name="load*" read-only="true" />  
  162.             <tx:method name="select*" read-only="true" />   
  163.             <tx:method name="count*" read-only="true" />    
  164.             <tx:method name="search*" read-only="true" />    
  165.             <tx:method name="list*" read-only="true" />    
  166.             <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />    
  167.         </tx:attributes>    
  168.     </tx:advice>    
  169.     <aop:aspectj-autoproxy proxy-target-class="true"/>    
  170.     <!-- 开启注解事务 只对当前配置文件有效 -->  
  171.       
  172.     <!-- 扫描注解Bean -->  
  173.     <context:component-scan base-package="com.xinnet">  
  174.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>    
  175.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>  
  176.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  177.     </context:component-scan>  
  178.      <!-- 隐式地向 Spring 容器注册 -->  
  179.     <context:annotation-config/>   
  180.       
  181. </beans>  


<2>这是第二个数据源 和第一个数据源一样 需要有事务 扫描注解
 不同的是数据源的 url  username 和password 用的是第二个数据源的连接  用户名和密码


[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:jdbc="http://www.springframework.org/schema/jdbc"   
  6.     xmlns:jee="http://www.springframework.org/schema/jee"  
  7.     xmlns:tx="http://www.springframework.org/schema/tx"   
  8.     xmlns:aop="http://www.springframework.org/schema/aop"  
  9.     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  10.     xsi:schemaLocation="  
  11.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  13.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd  
  14.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd  
  15.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
  16.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd  
  17.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
  18.         http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"  
  19.     default-lazy-init="true">  
  20.   
  21.     <description>Spring公共配置 </description>  
  22.       
  23.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  24.         <property name="locations">  
  25.             <list>  
  26.                 <value>classpath:resources.properties</value>  
  27.                 <!--  <value>classpath:memcached.properties</value> -->  
  28.             </list>  
  29.         </property>  
  30.     </bean>  
  31.       
  32.  <!-- **************** druid 监控连接池配置 *****************  -->  
  33.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  34.           
  35.         <!-- 基本属性 url、user、password -->  
  36.         <property name="url" value="${url2}" />  
  37.         <property name="username" value="${username2}" />  
  38.         <property name="password" value="${password2}" />  
  39.           
  40.         <!-- 配置初始化大小、最小、最大 -->  
  41.         <property name="initialSize" value="${initialSize}" />  
  42.         <property name="minIdle" value="${minIdle}" />  
  43.         <property name="maxActive" value="${maxActive}" />  
  44.           
  45.         <!-- 配置获取连接等待超时的时间 -->  
  46.         <property name="maxWait" value="${maxWait}" />  
  47.           
  48.         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
  49.         <property name="timeBetweenEvictionRunsMillis" value="60000" />  
  50.           
  51.         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
  52.         <property name="minEvictableIdleTimeMillis" value="300000" />  
  53.         <property name="validationQuery" value="SELECT 'x'" />  
  54.         <property name="testWhileIdle" value="true" />  
  55.         <property name="testOnBorrow" value="false" />  
  56.         <property name="testOnReturn" value="false" />  
  57.           
  58.         <!-- 打开PSCache,并且指定每个连接上PSCache的大小  -->  
  59.         <!-- 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false -->  
  60.         <property name="poolPreparedStatements" value="${poolPreparedStatements}" />  
  61.         <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />  
  62.           
  63.         <!-- 对泄漏的连接 自动关闭 -->  
  64.         <property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 打开removeAbandoned功能 -->  
  65.         <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 1800秒,也就是30分钟 -->  
  66.         <property name="logAbandoned" value="${logAbandoned}" /> <!-- 关闭abanded连接时输出错误日志 -->  
  67.           
  68.         <!-- 配置监控统计拦截的filters -->  
  69.         <property name="filters" value="mergeStat" />  
  70.         <!--  <property name="filters" value="stat" /> -->  
  71.         <!-- 慢日志查询  缺省为3秒  修改为10秒 10000 -->  
  72.         <property name="connectionProperties" value="druid.stat.slowSqlMillis=5000" />  
  73.           
  74.         <!-- DruidDataSource各自独立 , 支持配置公用监控数据 -->  
  75.         <!-- <property name="useGloalDataSourceStat" value="true" /> -->  
  76.     </bean>  
  77.       
  78.     <!-- druid 监控  spring  -->  
  79.     <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"/>  
  80.     <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">  
  81.         <property name="patterns">  
  82.             <list>  
  83.                 <value>com.xinnet.*.service.*</value>  
  84.             </list>  
  85.         </property>  
  86.     </bean>  
  87.     <aop:config>  
  88.         <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />  
  89.     </aop:config>  
  90.       
  91.     <!-- MyBatis Mapper.XMl 配置 -->  
  92.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  93.         <property name="dataSource" ref="dataSource" />  
  94.         <property name="configLocation" value="classpath:config/mybatis.xml" />  
  95.         <property name="mapperLocations">  
  96.             <list>  
  97.             <!-- 自动匹配Mapper映射文件  -->  
  98.                 <value>classpath:mapper/**/*-mapper.xml</value>  
  99.             </list>  
  100.         </property>  
  101.         <!-- 添加插件 -->  
  102.         <property name="plugins">  
  103.             <array>  
  104.                 <ref bean="pagePlugin" />  
  105.             </array>  
  106.         </property>  
  107.     </bean>  
  108.       
  109.     <!-- 支持文件上传相关 -->  
  110.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>  
  111.       
  112.     <!-- 分页插件,根据方言自动添加分页信息,默认要求 -->  
  113.     <bean id="pagePlugin" class="com.xinnet.core.mybatis.plugin.PagePlugin">  
  114.         <property name="properties">  
  115.             <props>  
  116.                 <prop key="dialect">com.xinnet.core.mybatis.dialet.MySQLDialect</prop>  
  117.                 <prop key="pageSqlId">.*query.*</prop>  
  118.             </props>  
  119.         </property>  
  120.     </bean>  
  121.     <!-- redis客户端 -->  
  122.      <!-- jedis pool配置  -->    
  123.     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">    
  124.         <property name="maxActive" value="${redis.maxActive}" />    
  125.         <property name="maxIdle" value="${redis.maxIdle}" />    
  126.         <property name="maxWait" value="${redis.maxWait}" />    
  127.         <property name="testOnBorrow" value="${redis.testOnBorrow}" />    
  128.     </bean>    
  129.       
  130.     <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">    
  131.         <property name="usePool" value="true"></property>    
  132.         <property name="hostName" value="${redis.host}" />    
  133.         <property name="port" value="${redis.port}" />    
  134.         <property name="password" value="${redis.pass}" />    
  135.         <property name="timeout" value="${redis.timeout}" />    
  136.         <property name="database" value="${redis.default.db}"></property>    
  137.         <constructor-arg index="0" ref="jedisPoolConfig" />    
  138.     </bean>    
  139.     <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">    
  140.         <property name="connectionFactory" ref="jedisConnectionFactory" />    
  141.     </bean>  
  142.      
  143.     <!-- ***************事务配置************** -->  
  144.     <tx:annotation-driven transaction-manager="transactionManager"/>  
  145.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  146.         <property name="dataSource" ref="dataSource" />  
  147.     </bean>  
  148.     <aop:config>    
  149.              <aop:advisor pointcut="execution(* com.xinnet..service..*.*(..))"  advice-ref="txAdvice" />    
  150.     </aop:config>    
  151.     <tx:advice id="txAdvice" transaction-manager="transactionManager">    
  152.         <tx:attributes>    
  153.             <tx:method name="get*" read-only="true" />    
  154.             <tx:method name="query*" read-only="true" />    
  155.             <tx:method name="find*" read-only="true" />    
  156.             <tx:method name="load*" read-only="true" />  
  157.             <tx:method name="select*" read-only="true" />   
  158.             <tx:method name="count*" read-only="true" />    
  159.             <tx:method name="search*" read-only="true" />    
  160.             <tx:method name="list*" read-only="true" />    
  161.             <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />    
  162.         </tx:attributes>    
  163.     </tx:advice>    
  164.     <aop:aspectj-autoproxy proxy-target-class="true"/>    
  165.     <!-- 开启注解事务 只对当前配置文件有效 -->  
  166.       
  167.     <!-- 扫描注解Bean -->  
  168.     <context:component-scan base-package="com.xinnet">  
  169.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>    
  170.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>  
  171.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  172.     </context:component-scan>  
  173.      <!-- 隐式地向 Spring 容器注册 -->  
  174.     <context:annotation-config/>   
  175.       
  176. </beans>  

<3>这个时候就可以用两个数据源进行切换注入 从而使用不同数据源的service

这是service里面的方法 该怎么写就是怎么写 该怎么注解就怎么注解 不用做特殊的步骤 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @Service("partyService")  
  2. public class PartyServiceImpl implements PartyService {  
  3.   
  4.     @Autowired  
  5.     private PartyDao partyDao;  
  6.       
  7.     @Override  
  8.     public List<Emp> getAllEmp() throws SQLException {  
  9.         return partyDao.getAllEmp();  
  10.     }  
  11.   
  12. }  
Dao层也是一样 该怎么写就怎么写 该怎么注解就怎么注解

然后可以写一个测试类试一下连接

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = { "classpath:spring.xml" })//这里的spring.xml加载了第一个数据源 spring-commons.xml   
  3. public class DataSourceTest extends AbstractTransactionalJUnit4SpringContextTests {  
  4.     /** 
  5.      *  
  6.      * 功能描述:xxxxx 
  7.      * 
  8.      * @throws SQLException 
  9.      * 
  10.      * @author xxx[973893384@163.com] 
  11.      * 
  12.      * @since 2014年3月12日 
  13.      * 
  14.      * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述] 
  15.      */  
  16.     @Test  
  17.     @Rollback(true)  
  18.     public void testGetAllEmp() throws SQLException {  
  19.         ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:config/hymanager/hymanager_spring.xml");//这里加载的是第二个数据源配置文件路径 使用的时候用application 读取第二个数据源的配置文件 就可以用getBean 获取注入的service 不用ac.getBean 用@Autowired自动注入的 则使用的是第一个数据源    
  20.         PartyService partyService = (PartyService) ac.getBean("partyService");  
  21.         List<Emp> list = partyService.getAllEmp();  
  22.         for (Emp emp : list) {  
  23.             System.out.println(emp.getEmpno());  
  24.         }  
  25.     }  

这样就可以封装一个父类


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class DataSourceChange {  
  2.     //第二个数据源数据源  
  3.     public ApplicationContext otherDataSource=new FileSystemXmlApplicationContext("classpath:config/hymanager/hymanager_spring.xml");  
  4.     //默认数据源  
  5.     public ApplicationContext defaultDataSource=new FileSystemXmlApplicationContext("classpath:config/spring_commons.xml");  
  6.     /** 
  7.      *  
  8.      * 功能描述:xxxx 
  9.      * 
  10.      * @param beanName 
  11.      * @return 
  12.      * 
  13.      * @author xxx[973893384@163.com] 
  14.      * 
  15.      * @since 2014年3月12日 
  16.      * 
  17.      * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述] 
  18.      */  
  19.     public Object now(String beanName) {  
  20.         return defaultDataSource.getBean(beanName);  
  21.     }  
  22.     public Object after(String beanName) {  
  23.         return otherDataSource.getBean(beanName);  
  24.     }  
  25. }  

要使用多个数据源的类就可以继承这个父类
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class HyMangerEmpDataHandle extends DataSourceChange {  
  2.     //用父类的after方法更换配置切换第二套数据源获取注入partyService  
  3.     private PartyService partyService=(PartyService) after("partyService");  
  4.       
  5.     //<span style="font-family: Arial, Helvetica, sans-serif;">用父类的now方法</span>更换配置切换第一套数据源获取注入empService  
  6.     private EmpService empService=(EmpService) now("empService");  
  7.       
  8.     private static Logger log=LoggerFactory.getLogger(PartyService.class);  
  9.     /** 
  10.      *  
  11.      * 功能描述:xxxxx 
  12.      * 
  13.      * @throws SQLException 
  14.      * 
  15.      * @author xxx[973893384@163.com] 
  16.      * 
  17.      * @since 2014年3月12日 
  18.      * 
  19.      * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述] 
  20.      */  
  21.     public void leadingAllEmp() throws SQLException {  
  22.         List<Emp> partyList=partyService.getAllEmp();//第二个数据源注入的service  
  23.         for(Emp emp:partyList) {  
  24.             empService.addEmp(emp);//第一个数据源注入的service  
  25.         }  
  26.     }  
  27. }  


0 0
原创粉丝点击