一,在tomcat的context文件中做如下配置

 <?xml version="1.0" encoding="UTF-8"?>
 <Context>
     <WatchedResource>WEB-INF/web.xml</WatchedResource>
 <Resource acquireIncrement="2" auth="Container" driverClass="oracle.jdbc.driver.OracleDriver" factory="com..seashell.encrypt.C3p0BeanFactory" jdbcUrl="jdbc:oracle:thin:@ip地址:1530:billdb" maxPoolSize="5" minPoolSize="3" name="/JDBC/ORG" password="" type="com.mchange.v2.c3p0.ComboPooledDataSource" user=""/>

 <Resource acquireIncrement="2" auth="Container" driverClass="oracle.jdbc.driver.OracleDriver" factory="com..seashell.encrypt.C3p0BeanFactory" jdbcUrl="jdbc:oracle:thin:@ip地址:1530:billdb" maxPoolSize="5" minPoolSize="3" name="/JDBC/WX-PORTAL" password="" type="com.mchange.v2.c3p0.ComboPooledDataSource" user=""/>      

 <Resource acquireIncrement="2" auth="Container" driverClass="oracle.jdbc.driver.OracleDriver" factory="com..seashell.encrypt.C3p0BeanFactory" jdbcUrl="jdbc:oracle:thin:@ip地址:1530:billdb" maxPoolSize="5" minPoolSize="3" name="/JDBC/" password="" type="com.mchange.v2.c3p0.ComboPooledDataSource" user=""/> 

 <Resource acquireIncrement="2" auth="Container" driverClass="oracle.jdbc.driver.OracleDriver" factory="com..seashell.encrypt.C3p0BeanFactory" jdbcUrl="jdbc:oracle:thin:@ip地址:1530:billdb" maxPoolSize="5" minPoolSize="3" name="/JDBC/" password="" type="com.mchange.v2.c3p0.ComboPooledDataSource" user=""/> 

 <Resource acquireIncrement="2" auth="Container" driverClass="oracle.jdbc.driver.OracleDriver" factory="com..seashell.encrypt.C3p0BeanFactory" jdbcUrl="jdbc:oracle:thin:@ip地址:1530:billdb" maxPoolSize="5" minPoolSize="3" name="/JDBC/" password="" type="com.mchange.v2.c3p0.ComboPooledDataSource" user=""/>      

 <Resource acquireIncrement="2" auth="Container" driverClass="oracle.jdbc.driver.OracleDriver" factory="com..seashell.encrypt.C3p0BeanFactory" jdbcUrl="jdbc:oracle:thin:@ip地址:1530:billdb" maxPoolSize="5" minPoolSize="3" name="/JDBC/" password="" type="com.mchange.v2.c3p0.ComboPooledDataSource" user=""/>      

 <Resource acquireIncrement="2" auth="Container" driverClass="oracle.jdbc.driver.OracleDriver" factory="com..seashell.encrypt.C3p0BeanFactory" jdbcUrl="jdbc:oracle:thin:@ip地址:1530:billdb" maxPoolSize="5" minPoolSize="3" name="/JDBC/" password="" type="com.mchange.v2.c3p0.ComboPooledDataSource" user=""/>           

 <Resource acquireIncrement="2" auth="Container" driverClass="org.gjt.mm.mysql.Driver" factory="com..seashell.encrypt.C3p0BeanFactory" idleConnectionTestPeriod="3600" jdbcUrl="jdbc:mysql://ip地址:3306/b2b_portal?useUnicode=true&amp;characterEncoding=UTF-8" maxIdleTime="3600" maxPoolSize="30" minPoolSize="3" name="/JDBC/-" password="" type="com.mchange.v2.c3p0.ComboPooledDataSource" user=""/>

 <Resource acquireIncrement="2" auth="Container" driverClass="org.gjt.mm.mysql.Driver" factory="com..seashell.encrypt.C3p0BeanFactory" idleConnectionTestPeriod="3600" jdbcUrl="jdbc:mysql://ip地址:3306/appmobile?useUnicode=true&amp;characterEncoding=UTF-8" maxIdleTime="3600" maxPoolSize="30" minPoolSize="3" name="/JDBC/" password="" type="com.mchange.v2.c3p0.ComboPooledDataSource" user=""/>

 <Resource acquireIncrement="2" auth="Container" driverClass="org.gjt.mm.mysql.Driver" factory="com..seashell.encrypt.C3p0BeanFactory" idleConnectionTestPeriod="3600" jdbcUrl="jdbc:mysql://ip地址:3306/oss?useUnicode=true&amp;characterEncoding=UTF-8" maxIdleTime="3600" maxPoolSize="30" minPoolSize="3" name="/JDBC/OSS" password="" type="com.mchange.v2.c3p0.ComboPooledDataSource" user=""/>

 <Resource acquireIncrement="2" auth="Container" driverClass="org.gjt.mm.mysql.Driver" factory="com..seashell.encrypt.C3p0BeanFactory" idleConnectionTestPeriod="3600" jdbcUrl="jdbc:mysql://ip地址:3306/boss?useUnicode=true&amp;characterEncoding=UTF-8" maxIdleTime="3600" maxPoolSize="30" minPoolSize="3" name="/JDBC/ORG" password="" type="com.mchange.v2.c3p0.ComboPooledDataSource" user=""/>

 </Context>

二,在初始化的xml中进行配置,注意它要被写在web.xml中的扫描到。

例如:web.xml中的配置

 <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>
            classpath:context/*.xml,
            classpath*:context/*.xml,
            classpath*:context/ibatis/*.xml,
         </param-value>
     </context-param>

然后在在context的文件下建立一个xml文件,注意这里不一定非的和项目名字一样,随便写就行了。

 <?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:p="http://www.springframework.org/schema/p"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
     <bean id="mobileDS" class="org.springframework.jndi.JndiObjectFactoryBean">
         <property name="jndiName" value="java:comp/env//JDBC/WX-PORTAL" />
     </bean>

     <bean id="bossDS" class="org.springframework.jndi.JndiObjectFactoryBean">
         <property name="jndiName" value="java:comp/env/JDBC/ORG" />
     </bean>

     <!-- b2b-portal 数据源配置 -->
     <bean id="dataSource" class="com..boss.ds.util.DynamicDataSource" >
         <property name="targetDataSources">
             <map key-type="java.lang.String">
                 <entry key="appmobile" value-ref="mobileDS" ></entry>
                 <entry key="org" value-ref="bossDS" ></entry>
             </map>
         </property>
     </bean>
     <!--事务管理器 -->
     <bean id="transactionManager"
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
         p:dataSource-ref="dataSource"/>

     <aop:config>
         <aop:advisor pointcut="execution(* com..*.service..*Service.*(..))"
             advice-ref="txAdvice" order="1" />
     </aop:config>

     <tx:advice id="txAdvice" transaction-manager="transactionManager">
         <tx:attributes>
             <tx:method name="save*" propagation="REQUIRED" />
             <tx:method name="update*" propagation="REQUIRED" />
             <tx:method name="del*" propagation="REQUIRED" />
             <tx:method name="change*" propagation="REQUIRED" />
             <tx:method name="create*" propagation="REQUIRED" />
         </tx:attributes>
     </tx:advice>  

 </beans>

这样就可以随意的切换数据源了。

JNDI在本项目中的应用的更多相关文章

  1. 转 web项目中的web.xml元素解析

    转 web项目中的web.xml元素解析 发表于1年前(2014-11-26 15:45)   阅读(497) | 评论(0) 16人收藏此文章, 我要收藏 赞0 上海源创会5月15日与你相约[玫瑰里 ...

  2. JNDI和在tomcat中配置DBCP连接池 元数据的使用 DBUtils框架的使用 多表操作

    1 JNDI和在tomcat中配置DBCP连接池 JNDI(Java Naming and Directory Interface),Java命名和目录接口,它对应于J2SE中的javax.namin ...

  3. 通过JNDI从服务器容器中获取资源_Spring JNDI+Mysql+Tomcat

    通过JNDI从服务器容器中获取DataSource资源 (由容器管理,不要关闭它,容器自己会处理)上一篇我们使用的是dbcp,这里使用JNDI: 使用JNDI连接数据: 在Spring可以注释 < ...

  4. tomcat项目中配置数据库连接池

    1. 在项目中新建context.xml文件,不要在tomcat服务器的目录中修改context.xml(会对整个服务器生效)..   在web项目的META-INF中存放context.xml 2. ...

  5. 【J2EE】在项目中理解J2EE规范

             J2EE平台由一整套服务(Service),应用程序接口(API)和协议构成,它对开发企业级应用提供了功能支持.13个核心技术各自是JDBC, JNDI, EJB, RMI, JSP ...

  6. VS项目中使用Nuget还原包后编译生产还一直报错?

    Nuget官网下载Nuget项目包的命令地址:https://www.nuget.org/packages 今天就遇到一个比较奇葩的问题,折腾了很久终于搞定了: 问题是这样的:我的解决方案原本是好好的 ...

  7. ABP项目中使用Swagger生成动态WebAPI

    本文是根据角落的白板报的<使用ABP实现SwaggerUI,生成动态webapi>一文的学习总结,感谢原文作者角落的白板报. 1 安装Swashbuckle.core 1.1 选择WebA ...

  8. iOS 之项目中遇到的问题总结

    昨天去一家公司面试,面试官问了我在项目开发中遇到过哪些问题,是什么引起的,怎样解决的? 当时由于有点小紧张只说出了一两点,现在就来好好总结一下. 问题: 1.两表联动 所谓的两表联动就是有左右两个表格 ...

  9. My97DatePicker时间控件在项目中的应用

    一.下载My97DatePicker的压缩包My97DatePicker.rar,解压. 注:My97DatePicker最新版本有开发包,项目中使用时删掉,以便节省空间,提高程序的运行效率. 二.在 ...

随机推荐

  1. BZOJ 2463: [中山市选2009]谁能赢呢?[智慧]

    明和小红经常玩一个博弈游戏.给定一个n×n的棋盘,一个石头被放在棋盘的左上角.他们轮流移动石头.每一回合,选手只能把石头向上,下,左,右四个方向移动一格,并且要求移动到的格子之前不能被访问过.谁不能移 ...

  2. BZOJ 1018: [SHOI2008]堵塞的交通traffic [线段树 区间信息]

    1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 3064  Solved: 1027[Submi ...

  3. LeetCode - 520. Detect Capital

    Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...

  4. 随机手机号和身份证号码(python)

    在使用selenium2 python自动化过程中,用户添加的时候程序设置的手机号和身份证号码是唯一的,这方面python代码可以实现,以下是调试成功,可以实现的. 具体代码如下 身份证需要下载dis ...

  5. 用UltraISO制作CentOS U盘安装盘

    1    下载UltraISO 网上有很多版本,下个绿色版的就ok了. 下载地址:http://www.pc6.com//softview/SoftView_13698.html 2    下载Cen ...

  6. Go学习笔记01-语言

    1.1 变量 Go 是静态类型语言,不能在运行期改变变量类型.使用关键字 var 定义变量,自动初始化为零值.如果提供初始化值,可省略变量类型,由编译器自动推断. var x int var f fl ...

  7. SQL查询练习一(From LeetCode)

     请选用MySQL进行测试. 1.找出后一天比前一天温度高的日期(E) 思路:将该表进行自关联,然后选择出日期靠后的一天,最后选择温度高的日期 select Weather.Id from Weath ...

  8. CEF小白人系列1-认识CEF

    手头上有个项目需要做浏览器的相关功能,评估了几个嵌入式方案最后选定CEF作为开发基础. 在入坑新技术的时候第一选择是去官网学习,这是一个非常好的习惯. CEF官网(请科学上网) https://bit ...

  9. Spring Boot 注解详解

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  10. Linux常见目录及其作用

    在Linux操作系统中,所有文件和目录都被组织成一个以根节点开始的倒置的树状结构.如下图 系统一般以 / 来表示根目录.在根目录之下的可以是目录也可以是文件,而每一个目录中又可以包含子目录文件.如此反 ...