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.
*/
javax.annotation.Resource;
javax.servlet.http.HttpServletRequest;
cat.domain.User;
cat.service.UserService;
org.apache.ibatis.annotations.Param;
org.springframework.beans.factory.annotation.Autowired;
org.springframework.stereotype.Controller;
org.springframework.ui.Model;
org.springframework.web.bind.annotation.PathVariable;
org.springframework.web.bind.annotation.RequestMapping;
@Controller
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:
修改了配置文件:


启动成功,但是编译还是有红线。。。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
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 也指向这个包