错误信息:
Exception in thread "main" java.lang.IllegalArgumentException: No SessionFactory specified
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:283)
at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:235)
at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:470)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:405)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:917)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:913)
at com.ourchr.channy.daoImp.UserDAO.findById(UserDAO.java:151)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy0.findById(Unknown Source)
at com.ourchr.channy.daoImp.UserDAO.main(UserDAO.java:163)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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/study_h" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="15" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED"></tx:method>
<tx:method name="find*" read-only="true" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="daoPointcut"
expression="execution(* com.ourchr.channy.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoPointcut" />
</aop:config> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">
</property>

<property name="mappingResources">
<list>
<value>com/ourchr/channy/entity/Directory.hbm.xml</value>
<value>com/ourchr/channy/entity/MyFile.hbm.xml</value>
<value>com/ourchr/channy/entity/User.hbm.xml</value>
<value>com/ourchr/channy/entity/Privilege.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userBO" class="com.ourchr.channy.bo.UserBO">
<property name="userDao" ref="userDAO"></property>
</bean>
<bean id="fileBO" class="com.ourchr.channy.bo.MyFileBO"></bean>
<bean id="dirBO" class="com.ourchr.channy.bo.MyDirectoryBO"></bean>
<bean id="privilegeBO" class="com.ourchr.channy.bo.MyPrivilegeBO"></bean>
<bean id="userDAO" class="com.ourchr.channy.daoImp.UserDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="fileDAO" class="com.ourchr.channy.daoImp.MyFileDAO"></bean>
<bean id="dirDAO" class="com.ourchr.channy.daoImp.MyDirectoryDAO"></bean>
<bean id="privilegeDAO"
class="com.ourchr.channy.daoImp.PrivilegeDAO">
</bean>
</beans>UserDAO
package com.ourchr.channy.daoImp;import java.sql.SQLException;
import java.util.Date;
import java.util.List;import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;import com.ourchr.channy.dao.IUserDAO;
import com.ourchr.channy.entity.User;@SuppressWarnings("unchecked")
public class UserDAO extends  BaseDAO<User> implements IUserDAO{
private static HibernateTemplate template;
public void setSessionFactory(SessionFactory sessionFactory) {
    template = new HibernateTemplate();
} public UserDAO() {
super(User.class);
}
/*
 * 将指定用户的最大存储空间修改为newSize
 */

public void updateStoreSize(User user,int newSize) {
Object[] values = new Object[2];
values[0] = newSize;
values[1] = user.getId();
template.find("update Privilege p set p.maxScoresize=? where p.id=?",values);
}

/*
 * 删除用户,同时删除用户创建的所有文件夹及所有文件
 */
public void deleteById(Long id) {
template.find("delete from User u WHERE u.id=?",id); }
/*
 *  根据用户id查询用户信息,同时查询出用户创建的文件夹
 */
public User findUserWithDirectories(Long id) {
User user = (User) template.find("FROM User u left join fetch u.directories WHERE u.id=?",id);
return user;


/*
 * --查询账号已过期的用户
 */
public List<User> findExpiredUsers() {
List<User> users = template.find("FROM User u left join u.privilege up WHERE up.expiration<?",new Date());
return users;

}

/*
 * --查询存储空间最大的用户信息
 */
public List<User> findHasMaxStoreSizeUser() {
List<User> users = template.find("FROM User u left join fetch u.privilege up WHERE up.maxScoresize=(SELECT max(p.maxScoresize) FROM Privilege p)");
return users;

}
public User findById(Long id){
User user=null;
List<User> users=template.find("from User u where u.id = ?",id);
if(users.size()!=0){
user=(User)(users.toArray()[0]);
}
return user;
}

public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserDAO dao = (IUserDAO) ac.getBean("userDAO");
dao.findById(1L);
}
}麻烦csdn的各位大侠们了!!!

解决方案 »

  1.   


    public class UserDAO extends BaseDAO<User> implements IUserDAO{
    private static HibernateTemplate template;
    private SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory sessionFactory) {
    template = new HibernateTemplate();
    }
      

  2.   

    没有注入sessionFactory,你spring的配置文件里面配了这个sessionFactory,但是你通过set方法注入时候,首先要声明它啊
      

  3.   

    而且配置文件中要加上default-autowire="byName",这样spring才会通过配置的bean id去初始化对象
      

  4.   

    我配置了property,不就不需要加default-autowire="byName"了吗?
    还有就是,我加了default-autowire="byName"还是出了一样的错误
    ps:我初学框架,不懂的很多,谢谢帮助!
      

  5.   

    楼主 看看你着几句代码
    template = new HibernateTemplate();这样 new 出来的 HibernateTemplate() 和你注入的 sessionFactory 没有一点关系啊
    最起码要加上 template.setSessionFactory(sessionFactory);吧??