关于tomcat 启动时有关 spring 的报错情况

bigkai12138 2019-07-09 08:28:44
我的代码在 springmvc 的单元测试里面可以通过。但是启动了 tomcat 时就会报错,报错信息如下:
No qualifying bean of type 'cn.crud.service.EmployeeService' available

下面是我的 applicationContext.xml 即 spring 相关代码:
 <!--Spring 的配置文件,配置和业务逻辑有关的-->
<context:component-scan base-package="cn.crud">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>


下面是我的 springmvcTest 代码:
 @Test
public void testPage(){
try {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")).andReturn();
//请求成功之后请求域中会有 pageInfo,我们可以取出出pageInfo进行验证
MockHttpServletRequest request = mvcResult.getRequest();
PageInfo pageInfo = (PageInfo)request.getAttribute("pageInfo");
System.out.println("当前页码:" + pageInfo.getPageNum());
System.out.println("总页码:" + pageInfo.getPages());
System.out.println("总记录数:" + pageInfo.getTotal());
System.out.println("在页面需要连续显示的页码:");
int[] nums = pageInfo.getNavigatepageNums();
for(int i : nums){
System.out.print(" "+ i);
}
System.out.println("");
//获取员工数据
List<Employee> list = pageInfo.getList();
for (Employee employee : list){
System.out.println("ID:" + employee.getEmpId() + "-------name:" + employee.getEmpName());
}
} catch (Exception e) {
e.printStackTrace();
}
}


然后是我 cn.crud.service包下的 EmployeeService
@Service
public class EmployeeService {

@Autowired
EmployeeMapper employeeMapper;


/**
* 查询所有员工
* @return
*/

public List<Employee> getAll(){
return employeeMapper.selectByExampleWithDept(null);
}
}


这里是我 cn.crud.controller.EmployeeController 代码
@Controller
public class EmployeeController {

@Autowired
EmployeeService employeeService;
/*
* 查询员工数据
* @return
*/
@RequestMapping("/emps")
public String getEmps(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model
model){
//在查询之前调用,传入页码以及每页的大小
PageHelper.startPage(pn,5);
List<Employee> emps = employeeService.getAll();
//用 pageInfo 包装查询后的结果,只需要将 pageInfo 交给页面就可以了
//封装了详细的分页信息,包括查询出来的数据,传入连续显示的页数
PageInfo page = new PageInfo(emps,5);
model.addAttribute("pageInfo",page);
return "list";
}

}


我后面尝试将 EmployeeService 类中的 Service 注解改成 @Controller 注解,但是在启动 tomcat 时就报错 No qualifying bean of type 'cn.crud.dao.EmployeeMapper ' available (PS:EmployeeMapper 是我通过 mybatis 逆向工程生成的,在测试的时候没有问题)
再次说明一下我的问题:
我的代码在 springmvc 的单元测试里面可以通过。但是启动了 tomcat 时就会报错,报错信息如下:
No qualifying bean of type 'cn.crud.service.EmployeeService' available

如果有人能帮忙解答的话我不胜感激。小弟先在此跪谢各位了
...全文
82 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧