ssh启动dao层找不到

发布于 2021-12-01 22:55:36 字数 9175 浏览 803 评论 6

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.orm.hibernate3.HibernateTemplate cuteshop.sys.dao.UserDao.hibernateTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.orm.hibernate3.HibernateTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at cuteshop.test.Provider.main(Provider.java:7)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.orm.hibernate3.HibernateTemplate cuteshop.sys.dao.UserDao.hibernateTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.orm.hibernate3.HibernateTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 13 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.orm.hibernate3.HibernateTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)

... 15 more


这是我的配置文件:

spring-hibernate.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">








<context:annotation-config />
<context:component-scan base-package="cuteshop.*"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${dataSource.driverClassName}" />
<property name="url" value="${dataSource.url}" /> 
<property name="username" value="${dataSource.username}" /> 
<property name="password" value="${dataSource.password}" /> 
</bean>
 <!-- 配置Hibernate的SessionFactory,通过spring提供的SessionFactoryBean配置-->  
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 配置依赖的数据源属性 -->
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${dataSource.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${dataSource.hbm2ddl.auto}</prop> 
                <prop key="hibernate.format_sql">true</prop> 
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>cuteshop.sys.entity</value><!-- 扫描实体类,也就是平时所说的model -->
</list>
</property>
</bean>


     <!-- 配置 Spring 的声明式事物 -->  
   <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="get*" read-only="true"/>  
          <tx:method name="purchase" propagation="REQUIRES_NEW"/>  
          <tx:method name="*"/>  
       </tx:attributes>  
   </tx:advice> 
   
</beans>

这是整个工程的目录结构

package cuteshop.sys.dao;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;


import cuteshop.sys.entity.UserInfo;
@Repository
public class UserDao{
@Autowired
private HibernateTemplate hibernateTemplate;
public void save(UserInfo userinfo) {
hibernateTemplate.save(userinfo);

}
public void update(UserInfo userinfo) {
hibernateTemplate.update(userinfo);

}
public void delete(UserInfo userinfo) {
hibernateTemplate.delete(userinfo);
}

public List<UserInfo> login(UserInfo u) {
@SuppressWarnings("unchecked")
List<UserInfo> list =  (List<UserInfo>) hibernateTemplate.find(
"from UserInfo u where u.user_name=? and u.pwd=?",u.getUsername(),u.getPassord());
if (list.size() > 0) {
return list;
}
return null;
}
}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

落墨 2021-12-03 19:18:06

一眼看出,:)

凯凯我们等你回来 2021-12-03 19:16:40

引用来自“阿信sxq”的评论

错误日志已经说的很明确了,缺少hibernateTemplate实例,你没有配置的,添加如下的配置到spring的配置文件

<bean
        id="hibernateTemplate"
        class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property
            name="sessionFactory"
            ref="sessionFactory" />
    </bean>

旧城烟雨 2021-12-03 19:14:59

什么意思 ,不行吗

深巷少女 2021-12-03 18:54:00

可以的。你的自由。

三月梨花 2021-12-03 16:31:54

这代码贴的也是够醉了。

偏爱自由 2021-12-03 05:33:48

错误日志已经说的很明确了,缺少hibernateTemplate实例,你没有配置的,添加如下的配置到spring的配置文件

<bean
        id="hibernateTemplate"
        class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property
            name="sessionFactory"
            ref="sessionFactory" />
    </bean>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文