以前没弄过spring的东东,最近有需要用spring弄报表相关的东东。这不,报表功能还没添加,各种配置问题,各种奇奇怪怪的问题出现了,“新手定理”被完美诠释了。
一直报错,不懂什么意思。明明已经写了自动注入和组件标记。
错误信息如下:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.cn.wuxiong.spring.school.dao.StudentDao] 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)}
对应的java实体类如下:
一直报错,不懂什么意思。明明已经写了自动注入和组件标记。
错误信息如下:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.cn.wuxiong.spring.school.dao.StudentDao] 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)}
对应的java实体类如下:
package com.cn.wuxiong.spring.school.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@Entity
@Table(name = "aastudent", catalog = "itsm")
@NamedQueries({
@NamedQuery(name = "findAllStudents", query = "SELECT s FROM Student s"),
@NamedQuery(name = "findStudentById", query = "SELECT s FROM Student s where s.id=?") })
public class Student {
private Long id;
private String stuNo;
private String name;
private Integer age;
private String sex;
public Student() {
};
public Student(Long id, String stuNo, String name, Integer age, String sex) {
this.id = id;
this.stuNo = stuNo;
this.name = name;
this.age = age;
this.sex = sex;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "stuNo", nullable = false)
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
@Column(name = "name", nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "age", nullable = false)
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Column(name = "sex", nullable = false)
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
============================================================================
然后对应的dao,这个类里面没什么具体的方法,只是个接口,方法都在上一级接口中:
package com.cn.wuxiong.spring.school.dao;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.cn.wuxiong.spring.school.domain.Student;
public interface StudentDao extends PagingAndSortingRepository<Student, Long>,StudentDaoEx {//
}
========
对应的StudentDaoEx 接口:
package com.cn.wuxiong.spring.school.dao;
import org.springframework.stereotype.Repository;
import com.cn.wuxiong.spring.school.domain.Student;
@Repository
public interface StudentDaoEx {
public int getCount();
public void persist(Student st);
public void testFunc();
}
===========
对应的上一个接口的实现类如下,为了测试,故而写了相当少的测试方法:
package com.cn.wuxiong.spring.school.dao;
import org.springframework.stereotype.Repository;
import com.cn.wuxiong.spring.school.domain.Student;
@Repository
public class StudentDaoImpl implements StudentDaoEx {
@Override
public void testFunc(){
System.out.println("#############Test DAO");
}
public void persist(Student st){
System.out.println(st);
}
public int getCount(){
System.out.println("数量为:3");
return 3;
}
}
=========================================================================
而后对应的service如下:
package com.cn.wuxiong.spring.school.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.cn.wuxiong.spring.school.dao.StudentDao;
@org.springframework.stereotype.Service
@Transactional
public class SchoolService {
@Autowired
private StudentDao dao;
public void testService() {
// dao.testFunc();
System.out.println("###########################Test service");
}
}
==========================================================
对应的controller为:
package com.cn.wuxiong.spring.school.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.cn.wuxiong.spring.school.service.SchoolService;
@Controller
@RequestMapping(value = "/itsm/school")
public class SchoolCtrl {
@Autowired
private SchoolService schoolService;
@RequestMapping(value="/student.do", method = RequestMethod.GET)
public String pagingList() {
schoolService.testService();
return "school/studentManagement";
}
// 新建
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createPage(Model model) {
model.addAttribute("action_type", "create");
return "school/add_update_student";
}
}
解决方案