一,在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&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&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&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&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>
这样就可以随意的切换数据源了。