求大神解答,试了很多方法都没解决。
相关bean.xml:
<bean id="dataSource" class="com.mysql.jdbc.Driver"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="100" />
<property name="maxidle" value="20" />
<property name="maxWait" value="1000" />
</bean>
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packageToScan">
<value>spring.mvc.model</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.fomrat_sql">false</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>
Dao类
package spring.mvc.dao.imp;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import spring.mvc.dao.UserDao;
import spring.mvc.model.Pager;
import spring.mvc.model.SystemContext;
import spring.mvc.model.User;
@SuppressWarnings("deprecation")
@Repository("userDaoImp")
public class UserDaoImp extends HibernateDaoSupport implements UserDao {
@Resource(name="sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory) {
this.setSessionFactory(sessionFactory);
}
@Override
public void add(User user) {
this.getHibernateTemplate().save(user);
}
@Override
public void update(User user) {
this.getHibernateTemplate().update(user);
}
@Override
public void delete(int id) {
User user = this.load(id);
this.getHibernateTemplate().delete(user);
}
@Override
public User load(int id) {
return this.getHibernateTemplate().load(User.class, id);
}
@SuppressWarnings("unchecked")
@Override
public List<User> list() {
return this.getSession().createQuery("from User").list();
}
@SuppressWarnings("unchecked")
@Override
public Pager<User> find() {
int size = SystemContext.getSize();
int offset = SystemContext.getOffset();
Query query = this.getSession().createQuery("from User");
query.setFirstResult(offset).setMaxResults(size);
List<User> datas = query.list();
Pager<User> us = new Pager<User>();
us.setDatas(datas);
us.setOffset(offset);
us.setSize(size);
long total = (long) this.getSession()
.createQuery("select count(*) from User").uniqueResult();
us.setTotal(total);
return us;
}
@Override
public User loadByUsername(String username) {
return (User) this.getSession().createQuery("from User where username=?").setParameter(0, username).uniqueResult();
}
}
异常:
严重: Servlet.service() for servlet [user] in context with path [/spring_user] threw exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:701)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1082)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:276)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:261)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:192)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
@Resource(name="sessionFactory")
publicvoidsetSuperSessionFactory(SessionFactorysessionFactory){
this.setSessionFactory(sessionFactory);
}
换成
@Resource(name="sessionFactory")
private SessionFactorysessionFactory;
publicvoidsetSuperSessionFactory(){
this.setSessionFactory(sessionFactory);
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。