Spring + Hibernate 配置中 始终不能注入SessionFactory(分不多请见谅)
在使用SSH的配置中,Spring的配置中dataSource,SessionFactory的配置都没有问题,可是在Java程序中得到的SessionFactory始终未空。(java的DAO中直接定义SessionFactory属性)
怀疑是SessionFactory属性没有被注入。
那位高手有合适的建议?
和书中的例子一模一样。是不是还需要定义事务?
如果我只想处理SessionFactory,不定义事务可以吗?
请赐教。
[解决办法]
不定义事务可以,但是数据只能读取,Hibernate需要事务提交数据至库。
帖你代码看看。
你先写个测试类看看是不是包有问题。
[解决办法]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/bookstore</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>mxg</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.MySQLDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>org/mxg/vo/Ordersitem.hbm.xml</value> <value>org/mxg/vo/Catalog.hbm.xml</value> <value>org/mxg/vo/Book.hbm.xml</value> <value>org/mxg/vo/User.hbm.xml</value> <value>org/mxg/vo/Orders.hbm.xml</value> </list> </property> </bean> <bean id="ordersitemDAO" class="org.mxg.dao.impl.OrdersitemDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="catalogDAO" class="org.mxg.dao.impl.CatalogDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="bookDAO" class="org.mxg.dao.impl.BookDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="userDAO" class="org.mxg.dao.impl.UserDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="ordersDAO" class="org.mxg.dao.impl.OrdersDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="userService" class="org.mxg.service.impl.UserService"> <property name="userDAO"> <ref bean="userDAO" /> </property> </bean> <bean id="bookService" class="org.mxg.service.impl.BookService"> <property name="bookDAO"> <ref bean="bookDAO" /> </property> </bean> <bean id="catalogService" class="org.mxg.service.impl.CatalogService"> <property name="catalogDAO"> <ref bean="catalogDAO" /> </property> </bean> <bean id="ordersService" class="org.mxg.service.impl.OrdersService"> <property name="ordersDAO"> <ref bean="ordersDAO" /> </property> </bean> <bean name="/login" class="org.mxg.struts.action.LoginAction"> <property name="userService"> <ref bean="userService" /> </property> </bean> <bean name="/register" class="org.mxg.struts.action.RegisterAction"> <property name="userService"> <ref bean="userService" /> </property> </bean> <bean name="/getAllCatalogs" class="org.mxg.struts.action.GetAllCatalogsAction"> <property name="catalogService"> <ref bean="catalogService" /> </property> </bean> <bean name="/findBooks" class="org.mxg.struts.action.FindBooksAction"> <property name="bookService"> <ref bean="bookService" /> </property> </bean> <bean name="/addToCart" class="org.mxg.struts.action.AddToCartAction"> <property name="bookService"> <ref bean="bookService" /> </property> </bean> <bean name="/updateCart" class="org.mxg.struts.action.UpdateCartAction"> </bean> <bean name="/checkout" class="org.mxg.struts.action.CheckoutAction"> <property name="ordersService"> <ref bean="ordersService" /> </property> </bean> <bean name="/serchBooks" class="org.mxg.struts.action.SerchBooksAction"> <property name="bookService"> <ref bean="bookService" /> </property> </bean></beans>