我遇到的关于spring注解的问题
一、关于bean对象的参数为空的问题
我最开始通过@Repository来创建Mapper实现类的bean,结果报错了
nested exception is java.lang.IllegalArgumentException: Property
‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required
报错原因:是我Mapper实现类是继承了SqlSessionDaoSupport,其中有个参数SqlSessionFactoryTemplate没有注入,所以就报错了。
所以建议在通过继承SqlSessionDaoSupport来写mapper的实现类时,用xml文件来管理bean。
二、关于bean对象名字匹配问题
@Component
@Controller【Controller层】
@Service 【Service层】
@Repository 【Dao层】
四个注册bean对象的注解,其不带参数值的会,bean的id默认为以开头为小写的类的驼峰式命名,举个栗子:
UserServiceImpl,使用注解注册bean时,其bean的id时“userServiceImpl”。
好了,所到这里也没什么重要的,但是我们通常在获取bean对象的时候,我们一般是获取该bean的接口名,而不是实体类的名字,也就是"userService",所以这是就会出现如下错误
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘studentService’ available
其实遇到这种情况一般都是注册bean失败或者注册的和注入的bean名字不相同导致的
三、关于Controller层注入Service失败
这个问题在我打算第一次整合ssm框架,写个项目时出现的,困扰我了几日天,然后在网上查找很久才解决了,所以我打算在这提一下,免得记性不好的我又遇见这种折磨我的错误。OK,我们进入正题,直接上代码
Service层
@Service
public class StudentServiceImpl implements StudentService{
private StudentMapper studentMapper;
private RecordMapper recordMapper;
private BookMapper bookMapper;
@Override
public Student login(Map<String, Object> map) {
System.out.println("studentMapper:"+studentMapper);
return studentMapper.loginQuery(map);
}
@Override
public int updatePwd(Map<String, Object> map) {
return studentMapper.updatePwd(map);
}
@Override
public int updateInfo(Student student) {
return studentMapper.update(student);
}
@Override
public List<Map<String,Object>> borrowBook(Map<String, Object> map) {
//添加借书记录
map.put("rTime", TimeUtil.getCurrentDateAndTime());
map.put("rState","未还");
Record record = MapToClass.toRecord(map);
recordMapper.add(record);
//图书数量减一
map.put("bAvailable",(Integer) map.get("bAvailable")-1);
Book book = MapToClass.toBook(map);
bookMapper.update(book);
//返回结果
return recordMapper.query(map);
}
@Override
public List<Map<String, Object>> returnBook(Map<String, Object> map) {
//归还图书状态
map.put("rTime",TimeUtil.getCurrentDateAndTime());
map.put("rState","已还");
recordMapper.update(map);
//图书数量加一
map.put("bAvailable",(Integer) map.get("bAvailable")+1);
Book book = MapToClass.toBook(map);
bookMapper.update(book);
//返回结果
return recordMapper.query(map);
}
//set注入
@Autowired
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
@Autowired
public void setRecordMapper(RecordMapper recordMapper) {
this.recordMapper = recordMapper;
}
@Autowired
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
Controller层
@RestController
public class StudentController {
private StudentService studentService;
//学生登录
@RequestMapping("/student/login")
public String login(@RequestParam String sId,@RequestParam String sPwd){
Map<String,Object> map = new HashMap<>();
map.put("sId",Integer.valueOf(sId));
map.put("sPwd",sPwd);
Student student = studentService.login(map);
ObjectMapper mapper = new ObjectMapper();
String sInfo = null;
try {
sInfo = mapper.writeValueAsString(student);
}catch (JsonProcessingException e){
e.printStackTrace();
}
System.out.println(student);
return sInfo;
}
@Autowired
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
}
咋一看,该注入的都注入,该注册的都注册了,结果一启动Tomcat
直接报了一个很长的错误
Unsatisfied dependency expressed through method ‘setStudentService’ parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.hoary.service.StudentService’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
这个问题困扰了我很久,直到我到网上去查,才知道,原来是web.xml文件中加载的spring容器压根就没有service bean。
web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
然后我将springmvc-servlet.xml改成aplicaitonContext.xml就行了,其实问题很简单,但是简单的Bug也架不住学艺不精的我呀!
最后希望各位同僚大佬能看得起鄙人,有bug可以私信我,我可以来学习学习。嘻嘻🤭