首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

明明注入了SessionFactory 为什么还是报这样的异常呀?'sessionFactory' or 'hibernateTemplate' is req

2012-03-19 
明明注入了SessionFactory为什么还是报这样的错误呀?sessionFactory or hibernateTemplate is require

明明注入了SessionFactory 为什么还是报这样的错误呀?'sessionFactory' or 'hibernateTemplate' is required
applicationContext.xml

Java code
<?xml version="1.0" encoding="UTF-8"?><beans    xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">    <bean id="DataSource"        class="org.apache.commons.dbcp.BasicDataSource">        <property name="driverClassName"            value="com.mysql.jdbc.Driver">        </property>        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>        <property name="username" value="root"></property>        <property name="password" value="root"></property>    </bean>    <bean id="SessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="dataSource">            <ref bean="DataSource"></ref>        </property>        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">                    org.hibernate.dialect.MySQLDialect                </prop>            </props>        </property>        <property name="mappingResources">            <list>                <value>mybean/Message.hbm.xml</value>                <value>mybean/User.hbm.xml</value></list>        </property>    </bean>        <!-- 配置用户DAO组件 -->    <bean id="UserDaoImpl" class="daoimp.UserDaoImpl">        <property name="sessionfactory">            <ref bean="SessionFactory"/>        </property>    </bean>    <!-- 配置消息DAO组件 -->    <bean id="MessageDaoImpl" class="daoimp.MessageDaoImpl">        <property name="sessionfactory">            <ref bean="SessionFactory"/>        </property>        <property name="pageSize" value="2"/>    </bean>    <!-- 配置业务逻辑组件 -->    <bean id="MessageManagerBusinessImpl" class="businessimp.MessageManageerBusinessImpl">        <property name="userDao" ref="UserDaoImpl"></property>        <property name="messageDao" ref="MessageDaoImpl"></property>    </bean></beans>



UserDaoImpl.java

Java code
package daoimp;import java.util.List;import org.hibernate.SessionFactory;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import mybean.User;import dao.UserDao;public class UserDaoImpl extends HibernateDaoSupport implements UserDao {    private SessionFactory sessionfactory;    public SessionFactory getSessionfactory() {        return sessionfactory;    }    public void setSessionfactory(SessionFactory sessionfactory) {        this.sessionfactory = sessionfactory;    }    public UserDaoImpl() {        // TODO 自动生成构造函数存根    }    public void deleteUser(User u) {        getHibernateTemplate().delete(u);    }    public User findByUserName(String name) {        List<User> ul = getHibernateTemplate().find("from User as u where u.name = ?",name);        if(ul!=null && ul.size()>0){            return ul.get(0);        }        return null;    }    public User getUser(int id) {        return (User)getHibernateTemplate().get(User.class, new Integer(id));    }    public int saveUser(User u) {        getHibernateTemplate().save(u);        return u.getId();    }    public void updataUser(User u) {        getHibernateTemplate().saveOrUpdate(u);    }    public void deleteUser(int id) {        getHibernateTemplate().delete(getHibernateTemplate().get(User.class, new Integer(id)));    }} 






错误:
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserDaoImpl' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:115)

[解决办法]
你已经继承了HibernateDaoSupport

set方法可以不用写

你把 getHibernateTemplate()前面都加上个this关键字

因为你要用当前类的实例
[解决办法]
dao里面不需要写set方法了 你继承了hibernatedaosupport就可以不写 变量的命名。。最好规范!我以前也遇到过 就是因为命名不规范 找不到!
[解决办法]
你可以去看下HibernateDaoSupport 的源文件

在开头就是个final的setSessionFactory方法



之所以你改成大写可以是因为set方法后的 sessionFactory是大写(去掉set后S变小写我想你知道,就不解释了)

也就是说你声明的属性
private SessionFactory sessionfactory;其实没什么用 

至于你的一些零零碎碎的问题,等晚上有时间我按你的代码测试一下再看看吧

热点排行