JNDI在本项目中的应用

时间:2024-01-14 09:39:32

一,在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>

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