JNDI在本项目中的应用

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

 

假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击http://106.12.206.16:8080/qingruihappy/index.html

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <Context>
 3     <WatchedResource>WEB-INF/web.xml</WatchedResource>
 4 <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=""/>
 5 
 6 <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=""/>      
 7      
 8 <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=""/> 
 9 
10 <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=""/> 
11           
12 <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=""/>      
13      
14 <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=""/>      
15      
16 <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=""/>           
17      
18 <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=""/>
19 
20 <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=""/>
21 
22 <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=""/>
23     
24 <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=""/>
25         
26 
27 </Context>

 

 

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

 例如:web.xml中的配置

1 <context-param>
2         <param-name>contextConfigLocation</param-name>
3         <param-value> 
4            classpath:context/*.xml,
5            classpath*:context/*.xml,
6            classpath*:context/ibatis/*.xml,
7         </param-value>
8     </context-param>

 

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

 1 <?xml version="1.0" encoding="UTF-8"?> 
 2 
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="
 8             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 9             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
10             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
11             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
12     <bean id="mobileDS" class="org.springframework.jndi.JndiObjectFactoryBean">
13         <property name="jndiName" value="java:comp/env//JDBC/WX-PORTAL" />
14     </bean>
15     
16     <bean id="bossDS" class="org.springframework.jndi.JndiObjectFactoryBean">
17         <property name="jndiName" value="java:comp/env/JDBC/ORG" />
18     </bean>
19     
20     <!-- b2b-portal 数据源配置 -->
21     <bean id="dataSource" class="com..boss.ds.util.DynamicDataSource" >  
22         <property name="targetDataSources">  
23             <map key-type="java.lang.String">  
24                 <entry key="appmobile" value-ref="mobileDS" ></entry> 
25                 <entry key="org" value-ref="bossDS" ></entry>  
26             </map>  
27         </property>  
28     </bean>  
29     <!--事务管理器 -->
30     <bean id="transactionManager"
31         class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
32         p:dataSource-ref="dataSource"/>
33     
34     <aop:config>
35         <aop:advisor pointcut="execution(* com..*.service..*Service.*(..))"
36             advice-ref="txAdvice" order="1" />
37     </aop:config>
38     
39     <tx:advice id="txAdvice" transaction-manager="transactionManager">
40         <tx:attributes>
41             <tx:method name="save*" propagation="REQUIRED" />
42             <tx:method name="update*" propagation="REQUIRED" />
43             <tx:method name="del*" propagation="REQUIRED" />
44             <tx:method name="change*" propagation="REQUIRED" />
45             <tx:method name="create*" propagation="REQUIRED" />
46         </tx:attributes>
47     </tx:advice>  
48     
49 </beans>

 

 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值