Chinaunix首页 | 论坛 | 博客
  • 博客访问: 350777
  • 博文数量: 88
  • 博客积分: 1673
  • 博客等级: 上尉
  • 技术积分: 934
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-20 13:51
文章分类

全部博文(88)

文章存档

2016年(1)

2015年(4)

2014年(3)

2013年(7)

2012年(11)

2011年(1)

2009年(61)

我的朋友

分类: Java

2012-11-29 15:04:44

1。applicationContext.xml 配置

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.     <beans xmlns="" xmlns:xsi=""
  3.     >
  4.         xmlns:jee="" xmlns:tx=""
  5.         xmlns:context=""
  6.         xsi:schemaLocation="
  7.         /spring-beans-3.0.xsd
  8.         
  9.         /spring-tx-3.0.xsd
  10.         
  11.         /spring-jee-3.0.xsd
  12.         
  13.         /spring-context-3.0.xsd"
  14.         default-autowire="byName" default-lazy-init="true">
  15.       
  16.         <description>Spring公共配置 </description>
  17.       
  18.         <!-- 定义受环境影响易变的变量 -->
  19.         <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  20.             <property name="locations">
  21.                 <list>
  22.                     <value>classpath*:/jdbc.properties</value>
  23.                 </list>
  24.             </property>
  25.         </bean>
  26.       
  27.         <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
  28.         <context:component-scan base-package="org.gyfbao.office" />
  29.       
  30.         <!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
  31.         <bean id="dataSourceContent" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
  32.             <property name="uniqueResourceName">
  33.                 <value>jdbc/dataSourceContent</value>
  34.             </property>
  35.             <property name="xaDataSourceClassName">
  36.                 <value>oracle.jdbc.xa.client.OracleXADataSource</value>
  37.             </property>
  38.             <property name="xaProperties">
  39.                 <props>
  40.                     <prop key="URL">jdbc:oracle:thin:@localhost:1521:orcl</prop>
  41.                     <prop key="user">multiple</prop>
  42.                     <prop key="password">admin</prop>
  43.                 </props>
  44.             </property>
  45.             <property name="poolSize">
  46.                 <value>3</value>
  47.             </property>
  48.         </bean>
  49.         <bean id="dataSourceIndex" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
  50.             <property name="uniqueResourceName">
  51.                 <value>jdbc/dataSourceIndex</value>
  52.             </property>
  53.             <property name="xaDataSourceClassName">
  54.                 <value>net.sourceforge.jtds.jdbcx.JtdsDataSource</value>
  55.             </property>
  56.             <property name="xaProperties">
  57.                 <props>
  58.                     <prop key="serverName">localhost</prop>
  59.                     <prop key="portNumber">2433</prop>
  60.                     <prop key="databaseName">allinpay</prop>
  61.                     <prop key="user">sa</prop>
  62.                     <prop key="password">gyfbao</prop>
  63.                 </props>
  64.             </property>
  65.             <property name="poolSize">
  66.                 <value>3</value>
  67.             </property>
  68.         </bean>
  69.       
  70.       
  71.         <!-- Hibernate配置 -->
  72.         <bean id="sessionFactoryContent" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  73.             <property name="dataSource" ref="dataSourceContent" />
  74.             <property name="namingStrategy">
  75.                 <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
  76.             </property>
  77.             <property name="hibernateProperties">
  78.                 <props>
  79.                     <prop key="hibernate.dialect">${hibernate.orcl.dialect}</prop>
  80.                     <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
  81.                     <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
  82.                     <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
  83.                     </prop>
  84.                     <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
  85.                 </props>
  86.             </property>
  87.             <property name="packagesToScan" value="org.gyfbao.office" />
  88.         </bean>
  89.         <bean id="sessionFactoryIndex" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  90.             <property name="dataSource" ref="dataSourceIndex" />
  91.             <property name="namingStrategy">
  92.                 <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
  93.             </property>
  94.             <property name="hibernateProperties">
  95.                 <props>
  96.                     <prop key="hibernate.dialect">${hibernate.dialect}</prop>
  97.                     <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
  98.                     <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
  99.                     <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
  100.                     </prop>
  101.                     <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
  102.                 </props>
  103.             </property>
  104.             <property name="packagesToScan" value="org.gyfbao.office" />
  105.         </bean>
  106.          
  107.         <!-- 事务管理器配置,多数据源JTA事务-->
  108.          <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
  109.             <property name="forceShutdown"><value>true</value></property>
  110.         </bean>
  111.             
  112.         <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
  113.             <property name="transactionTimeout" value="300"/>
  114.         </bean>
  115.         <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
  116.             <property name="transactionManager" ref="atomikosTransactionManager" />
  117.             <property name="userTransaction" ref="atomikosUserTransaction"/>
  118.         </bean>
  119.          
  120.         <!-- 使用annotation定义事务 -->
  121.         <tx:annotation-driven transaction-manager="transactionManager" />
  122.       
  123.     </beans>
2。web.xml修改

点击(此处)折叠或打开

  1. <filter>
  2.            <filter-name>hibernateOpenSessionInViewFilterContent</filter-name>
  3.            <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  4.            <init-param>
  5.                <param-name>excludeSuffixs</param-name>
  6.                <param-value>js,css,jpg,gif</param-value>
  7.            </init-param>
  8.            <init-param>
  9.                   <param-name>sessionFactoryBeanName</param-name>
  10.                <param-value>sessionFactoryContent</param-value>
  11.            </init-param>
  12.     </filter>
  13.     <filter>
  14.            <filter-name>hibernateOpenSessionInViewFilterIndex</filter-name>
  15.            <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  16.            <init-param>
  17.                <param-name>excludeSuffixs</param-name>
  18.                <param-value>js,css,jpg,gif</param-value>
  19.            </init-param>
  20.            <init-param>
  21.                   <param-name>sessionFactoryBeanName</param-name>
  22.                <param-value>sessionFactoryIndex</param-value>
  23.            </init-param>
  24.     </filter>
  25.       
  26.       <filter-mapping>
  27.            <filter-name>hibernateOpenSessionInViewFilterContent</filter-name>
  28.            <url-pattern>
3。Dao代码中sessionFactory选择

点击(此处)折叠或打开

  1. @Repository
  2.     public class BrandDao extends HibernateDao<Brand,Long>{
  3.     @Override
  4.     @Resource(name = "sessionFactoryContent")
  5.     public void setSessionFactory(SessionFactory sessionFactory) {
  6.          super.setSessionFactory(sessionFactory);
  7.     }




阅读(2715) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

varyall2014-01-27 15:19:27

不错,刚刚那会由于使用了两个sessionfactory,结果一直提示,No seesion found这种错误,后来一看自己的web.xml只配置了一个OpenSessionInViewFilte,刚刚看到你的文章,在web.xl连又多加了一个OpenSessionInViewFilte。就欧凯了。真心好文