3
回答
ssh启动dao层找不到
【腾讯云】学生服务器套餐10元/月 >>>   
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;
}
}


package cuteshop.sys.service;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import cuteshop.sys.dao.UserDao;
import cuteshop.sys.entity.UserInfo;




@Service
@Transactional
public class UserService {
@Autowired
private UserDao userDao;
//增加用户
public void saveUser(UserInfo userinfo){
userDao.save(userinfo);
}
//查询所有用户
// public List<UserInfo> loadAllUsers(){
// return userDao.loadAll();
// }
//修改用户信息
public void update(UserInfo userinfo){
userDao.update(userinfo);
}
//删除用户信息
// public void deleteUsers(List<UserInfo> userinfos){
// userDao.deleteAll(userinfos);
// }
// public Page getSysParams(int pageNo, int pageSize, Object... conditions) {
// Page page = userDao.pagedQuery("from UserInfo u", pageNo,pageSize, conditions);
// return page;
// }


public List<UserInfo> login(UserInfo userinfo) {


return userDao.login(userinfo);

}
}



举报
顶部