背景:
项目库是A库,项目中某个模块需要获取B库的数据
环境:
MyEclipse2014 jdk1.6.0_45 tomcat6
要点:
1.配置文件结构
项目库是A库,项目中某个模块需要获取B库的数据
环境:
MyEclipse2014 jdk1.6.0_45 tomcat6
要点:
1.配置文件结构
2.bean的id
1.配置文件结构目录
manager对多个service进行管理,auth是单独出来的权限配置
2. applicationContext_Adata.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: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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<!-- <value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value> -->
<!-- <value>com.mysql.jdbc.Driver</value> -->
<!-- <value>oracle.jdbc.driver.OracleDriver</value> -->
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<!-- <value>jdbc:sqlserver://localhost:1433;DatabaseName=frame</value> -->
<!-- <value>jdbc:mysql://localhost:3306/frame?useOldAliasMetadataBehavior=true</value> -->
<!-- <value>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL = TCP)(HOST=localhost)(PORT=1521))(LOAD_BALANCE=YES))(CONNECT_DATA=(SERVICE=DEDICATED)(SERVICE_NAME=orcl)))</value> -->
<!-- <value>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL = TCP)(HOST=*.*.*.*)(PORT=1521))(LOAD_BALANCE=YES))(CONNECT_DATA=(SERVICE=DEDICATED)(SERVICE_NAME=orcl)))</value> -->
<value>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL = TCP)(HOST=*.*.*.*)(PORT=1521))(LOAD_BALANCE=YES))(CONNECT_DATA=(SERVICE=DEDICATED)(SERVICE_NAME=orcl)))</value>
<!-- <value>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL = TCP)(HOST=*.*.*.*)(PORT=1521))(LOAD_BALANCE=YES))(CONNECT_DATA=(SERVICE=DEDICATED)(SERVICE_NAME=orcl)))</value> -->
</property>
<property name="username">
<value>***</value>
<!-- <value>system</value> -->
</property>
<property name="password">
<value>***</value>
<!-- <value>123456</value> -->
</property>
<property name="maxActive">
<value>1000 </value>
</property>
<property name="maxIdle">
<value>500 </value>
</property>
<property name="maxWait">
<value>5000 </value>
</property>
<!--
<property name="validationQuery">
<value>select 1 from dual </value>
</property>
-->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
<!-- org.hibernate.dialect.SQLServerDialect -->
<!-- org.hibernate.dialect.MySQLDialect -->
<!-- org.hibernate.dialect.Oracle9Dialect -->
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<!-- 这里放hibernate的映射文件 -->
<value></value>
</list>
</property>
</bean>
<!-- transaction configration start -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="nestedTransactionAllowed">
<value>true</value>
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="bulkUpdate*" propagation="REQUIRED"/>
<tx:method name="merge*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="getSave*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- transaction configration end -->
<tx:annotation-driven/>
<bean id="hibernateDao" class="com.djzh.dao.impl.HibernateDAO" scope="prototype">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="manager" class="com.djzh.biz.impl.AbstractManager">
<property name="dao">
<ref bean="hibernateDao"/>
</property>
</bean>
<bean id="applicationContextUtil" class="com.djzh.common.util.ApplicationContextUtil">
</bean>
</beans>
3.applicationContext_Bdata.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: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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="rockDataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL = TCP)(HOST=*.*.*.*)(PORT=1521))(LOAD_BALANCE=YES))(CONNECT_DATA=(SERVICE=DEDICATED)(SERVICE_NAME=orcl)))</value>
</property>
<property name="username">
<value>***</value>
</property>
<property name="password">
<value>***</value>
</property>
<property name="maxActive">
<value>1000 </value>
</property>
<property name="maxIdle">
<value>500 </value>
</property>
<property name="maxWait">
<value>5000 </value>
</property>
</bean>
<bean id="rockSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="rockDataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<!-- 这里放hibernate的映射文件 -->
<value></value>
</list>
</property>
</bean>
<bean id="rockTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="rockSessionFactory" />
</property>
<property name="nestedTransactionAllowed">
<value>true</value>
</property>
</bean>
<tx:annotation-driven/>
<bean id="rockHibernateDao" class="com.djzh.dao.impl.HibernateDAO" scope="prototype">
<property name="sessionFactory">
<ref bean="rockSessionFactory" />
</property>
</bean>
<bean id="rockManager" class="com.djzh.biz.impl.AbstractManager">
<property name="dao">
<ref bean="rockHibernateDao"/>
</property>
</bean>
</beans>
4. applicationContext.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: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.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<import resource="applicationContext_auth.xml" />
<import resource="applicationContext_Adata.xml" />
<import resource="applicationContext_Bdata.xml" />
<import resource="applicationContext_service.xml" />
<import resource="applicationContext_manager.xml" />
<import resource="applicationContext_action.xml" />
</beans>
5. ApplicationContextUtil类
package com.zlwy.common.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context=applicationContext;
}
public static ApplicationContext getContext() {
return context;
}
}
希望对有需要的朋友一点儿帮助!