zjb11111222 2017-07-27 06:37 采纳率: 0%
浏览 1317

ssm整合报注入错误新手求大神解答

Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cn.me.service.UserService com.cn.me.controller.UserController.userservice; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cn.me.dao.Dao com.cn.me.service.UserServiceImp.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.cn.me.dao.Dao] 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)}
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cn.me.service.UserService com.cn.me.controller.UserController.userservice; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cn.me.dao.Dao com.cn.me.service.UserServiceImp.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.cn.me.dao.Dao] 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:531)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:295)
... 29 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cn.me.dao.Dao com.cn.me.service.UserServiceImp.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.cn.me.dao.Dao] 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)}

service接口
package com.cn.me.service;

import java.util.List;

import com.cn.me.pojo.User;

public interface UserService {
public List getall();

}

service实现类
package com.cn.me.service;

import java.util.List;

import javax.annotation.Resource;

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

import com.cn.me.dao.Dao;
import com.cn.me.pojo.User;
@Service
public class UserServiceImp implements UserService {

@Autowired
private Dao dao;
@Override
public List<User> getall() {
    // TODO Auto-generated method stub
    return dao.getall();
}

}

Controller
package com.cn.me.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.cn.me.pojo.User;
import com.cn.me.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userservice;

public String service(Map<String,Object>map)
{
    List<User> list=userservice.getall();
    map.put("allusers", list);
    return "list";
}

}

springmvc配置
<?xml version="1.0" encoding="UTF-8"?>
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/context/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/context/spring-aop-3.2.xsd ">

/context:component-scan
mvc:annotation-driven/

     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/view/"></property>
       <property name="suffix" value=".jsp"></property>
     </bean>  

    </beans>

spring配置
<?xml version="1.0" encoding="UTF-8"?>
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mybatis="http://www.springframework.org/schema/mybatis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mybatis
http://www.springframework.org/schema/mybatis/spring-mybatis-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/context/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/context/spring-aop-3.2.xsd ">

 <context:component-scan base-package="com.cn.me">

 </context:component-scan>
 <mvc:annotation-driven/>
    <!-- 数据源 -->

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >  
<property name="driver" value="org.gjt.mm.mysql.Driver"/>  
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>  
<property name="username" value="root"/>  
<property name="password" value=""/>     
</bean>


<!-- 事物控制 -->
  <bean id="transactionManager"  
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    <property name="dataSource" ref="dataSource" />  
</bean>


<!-- sqlsessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
<property name="dataSource" ref="dataSource"/>    
  <property name="mapperLocations" value="classpath:com/cn/me/dao/*.xml"></property> 
</bean>  


<bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"  value="com.cn.me.dao"></property>
 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
 </bean>
</beans>

web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">


org.springframework.web.context.ContextLoaderListener

<!-- 加载spring -->


contextConfigLocation

classpath:applicationContext.xml

  <welcome-file-list>index.jsp</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>  
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  


contextConfigLocation
classpath:springmvc.xml

1



spring
/

</web-app>
  • 写回答

3条回答 默认 最新

  • 一点寒芒先至 2017-07-27 07:43
    关注

    参考下这个,有现成模板
    你这个问题感觉是No qualifying bean of type [com.cn.me.dao.Dao]
    dao层出问题了

    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误