java - spring could not autowired field.
迷茫
迷茫 2017-04-17 17:39:06
[Java讨论组]

spring不能自动装配,这问题我在网上搜索了很久,但是都没有适合我的答案。
以下是报错信息

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cat.dao.UserMapper cat.service.UserServiceImpl.userMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cat.dao.UserMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: ...

    ... 61 more

目录结构:

UserController

  package cat.controller;

/**
    • Created by xugenli on 16/4/9.
      */

    1. javax.annotation.Resource;

    2. javax.servlet.http.HttpServletRequest;

    3. cat.domain.User;

    4. cat.service.UserService;

    5. org.apache.ibatis.annotations.Param;

    6. org.springframework.beans.factory.annotation.Autowired;

    7. org.springframework.stereotype.Controller;

    8. org.springframework.ui.Model;

    9. org.springframework.web.bind.annotation.PathVariable;

    10. org.springframework.web.bind.annotation.RequestMapping;

      @Controller

    11. class UserController {

         @Autowired
         private UserService userService;
      
         @RequestMapping(value = "/show/{id}")
         public String toIndex(@PathVariable("id") Integer id, Model model) {
             User user = userService.showUser(id);
             model.addAttribute("user", user);
             return "/showUser";
         }

      }

    UserMapper:

    package cat.dao;
    
    
    import cat.domain.User;
    
    public interface UserMapper {
        int deleteByPrimaryKey(Integer id);
    
        int insert(User record);
    
        int insertSelective(User record);
    
        User selectByPrimaryKey(Integer id);
    
        int updateByPrimaryKeySelective(User record);
    
        int updateByPrimaryKey(User record);
    }

    User:

    package cat.domain;
    
    public class User {
        private Integer id;
    
        private String userName;
    
        private String password;
    
        private Integer age;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName == null ? null : userName.trim();
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password == null ? null : password.trim();
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    }

    UserMapper.xml:

    
    
    
      
        
        
        
        
      
      
        id, user_name, password, age
      
        
      
        delete from user_t
        where id = #{id,jdbcType=INTEGER}
      
      
        insert into user_t (id, user_name, password, 
          age)
        values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
          #{age,jdbcType=INTEGER})
      
      
        insert into user_t
        
          
            id,
          
          
            user_name,
          
          
            password,
          
          
            age,
          
        
        
          
            #{id,jdbcType=INTEGER},
          
          
            #{userName,jdbcType=VARCHAR},
          
          
            #{password,jdbcType=VARCHAR},
          
          
            #{age,jdbcType=INTEGER},
          
        
      
      
        update user_t
        
          
            user_name = #{userName,jdbcType=VARCHAR},
          
          
            password = #{password,jdbcType=VARCHAR},
          
          
            age = #{age,jdbcType=INTEGER},
          
        
        where id = #{id,jdbcType=INTEGER}
      
      
        update user_t
        set user_name = #{userName,jdbcType=VARCHAR},
          password = #{password,jdbcType=VARCHAR},
          age = #{age,jdbcType=INTEGER}
        where id = #{id,jdbcType=INTEGER}
      
    
    
    
    
      
    

    UserService:

    package cat.service;
    
    import cat.domain.User;
    import org.springframework.stereotype.Repository;
    
    /**
     * Created by on 16/4/9.
     */
    public interface UserService {
        User showUser(int id);
    }

    UserServiceImpl:

    package cat.service;
    
    
    import cat.dao.UserMapper;
    import cat.domain.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    /**
     * Created by  on 16/4/9.
     */
    @Service
    public class UserServiceImpl {
    
        @Autowired
       private UserMapper userMapper;
    
    
        public User showUser(int id){
            return userMapper.selectByPrimaryKey(id);
        }
    }

    spring-mvc.xml:

    
    
        
        
        
        
            
                
                    text/html;charset=UTF-8
                
            
        
    
        
        
            
            
            
        
    
        
        
            
            
            
            
            
            
        
    
    
    
    
        
    
    
    
    

    spring-mybatis.xml:

    
    
        
        
    
    
    
        
        
            
        
    
        
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    
        
        
            
            
            
        
    
        
        
            
            
        
    
    
    
        
        
            
        
    
    
    
    
    
    
    

    这问题卡了挺久了,我知道就是某一处的小错误,不过就是找不出来=。=

    UPDATE:

    修改了配置文件:

    
    

    启动成功,但是编译还是有红线。。。

    迷茫
    迷茫

    业精于勤,荒于嬉;行成于思,毁于随。

    全部回复(3)
    巴扎黑

    spring-mybatis.xml里面的mapperScanner的basePackage应该是cat.dao

    伊谢尔伦

    报错上看是Mapper没创建成功。
    <property name="basePackage" value="dao" />
    这里配置有问题,value应该写上Mapper所在的包名,改为:
    <property name="basePackage" value="cat.dao" />

    黄舟

    mapper接口和XML放在同包,然后扫描basepackage 指向这个包,XML里namespace 也指向这个包

    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号