Spring包装Hibernate的话所有的基本配置都写在applicationContext.xml文件中.
<!-- (读) 数据源 -->
<bean id="readDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${readDataSource.driver}</value>
</property>
<property name="url">
<value>${readDataSource.url}</value>
</property>
<property name="username">
<value>${readDataSource.name}</value>
</property>
<property name="password">
<value>${readDataSource.password}</value>
</property>
</bean>
<!-- (写) 数据源 -->
<bean id="writeDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${writeDataSource.driver}</value>
</property>
<property name="url">
<value>${writeDataSource.url}</value>
</property>
<property name="username">
<value>${writeDataSource.name}</value>
</property>
<property name="password">
<value>${writeDataSource.password}</value>
</property>
</bean>
<!-- hibernate配置文件 -->
<bean id="hibernateconfig" class="org.springframework.core.io.ClassPathResource">
<constructor-arg value="hibernate.cfg.xml"></constructor-arg>
</bean>
<!-- (读)hibernate数据库链接 -->
<bean id="readSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="readDataSource"></property>
<property name="configLocation" ref="hibernateconfig"></property>
</bean>
<!-- (写)hibernate数据库链接 -->
<bean id="writeSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="writeDataSource"></property>
<property name="configLocation" ref="hibernateconfig"></property>
</bean>
<!-- (读)jdbc模板 -->
<bean id="readJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"><ref local="readDataSource"/></property>
</bean>
<!-- (写)jdbc模板 -->
<bean id="writeJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource"><ref local="writeDataSource"/></property>
</bean>
我遇到的情况是配置完后tomcat6能启动,但是访问是就抛出no bean named 'sessionFactory'is defined
原因是在web.xml中有配置sessionFilter的过滤器,它用来控制session的开关,它指向的类是
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
这个类中有个setSessionFactoryBeanName的方法默认值是“sessionFactory”,而我把sessionFactory的名字改为了readSessionFactory和wirteSessionFactory。
这时可以去web.xml中设置sessionFactory的beanName为
readSessionFactory或wirteSessionFactory,但是只能设置一个值,而我要用的是双数据库。最后的解决方案是我在web.xml配置了两个filter把sessionFactoryBeanName初始化成不同的值代码如下
<filter>
<filter-name>readSessionFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>readSessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>readSessionFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter>
<filter-name>writeSessionFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>writeSessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>writeSessionFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
这时你在配置dao时就可选择不同的sessionFactory,而且他们的session就交由不同的filter控制开关了。
分享到:
相关推荐
自己研究了一个多星期了,咨询了好多高手,也看了好几个视频,终于做成了S2SH的整合。我的平台是:MyEclipse6.0、Sql Server 2000、Tomcat6.0。压缩包里是整个项目,带jar包,可以直接导入,但数据库得自己按照...
struts2.0+hibernate3.0+spring3.0的网上调查系统,数据库是mysql 设计主要功能: 管理员:登陆、调查项目的增加(调查有效时间的设置)、调查问题的增加(单选和多选)、选择支的增加、结果统计(提供页面显示...
jar包:项目中除了使用了struts2,hibernate3.0和spring1.8以外(用spring2.0的包也可以.不能低于1.8的包.)还是用了junit,ajax,第三方的table组件.等等.所以需要下载相对应的包. 为了上传jar.我专门申请了一个网盘.所有...
Spring 采用 2.0 版本 Hibernate 采用3.0版本 ====================================================== 请注意:如出现中文乱码,检查如下配置是否正确。 (1)MySql 数据库是否是utf-8 格式(在安装时选择支持多...
技术:ExtJS 3.2 DWR 3.0 Struts 2.16 Spring 2.0 Hibernate 3.2 MySql 5.0 原文地址: http://yourgame.javaeye.com/blog/681537 火狐浏览器,谷歌浏览器测试通过. 讨论Q_群:91986229(群3) 76202406(群2) 62150370...
Spring 采用 2.0 版本 Hibernate 采用3.0版本 ====================================================== 请注意:如出现中文乱码,检查如下配置是否正确。 (1)MySql 数据库是否是utf-8 格式(在安装时选择支持多...
313 17.6 常用的JPA注释 316 17.7 小结 318 第18章 Hibernate 319 18.1 Hibernate体系结构 319 18.2 Hibernate核心接口 321 18.3 一个简单的Hibernate例子 321 18.4 详解Hibernate配置文件 325 18.4.1 Hibernate的两...
由于J2EE的开源的框架中提供了MVC模式实现框架Struts、对象关系模型中的Hibernate 的框架及拥有事务管理和依赖注入的Spring。利用现存框架可以更快开发系统。所以选择Java技术作为blog 的开发工具。 为了增加系统的...