1. 新建接口

main: com.csj2018.o2o.service/AreaService.java

package com.csj2018.o2o.service;
import java.util.List;
import com.csj2018.o2o.entity.Area;
public interface AreaService {
List<Area> getAreaList();
}

2. 新建实现类

main: com.csj2018.o2o.service.impl/AreaServiceImpl.java

package com.csj2018.o2o.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.csj2018.o2o.dao.AreaDao;
import com.csj2018.o2o.entity.Area;
import com.csj2018.o2o.service.AreaService;
@Service
public class AreaServiceImpl implements AreaService{
@Autowired
private AreaDao areaDao;
@Override
public List<Area> getAreaList(){
return areaDao.queryArea();
}
}

3. 修改基类

test: com.csj2018.o2o/BaseTest.java

package com.csj2018.o2o;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* 配置spring和junit整合,junit启动式加载springIOC容器
*/
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring配置文件的位置
@ContextConfiguration({"classpath:spring/spring-dao.xml","classpath:spring/spring-service.xml"})
public class BaseTest { }

4. 测试类

test: com.csj2018.o2o.service/AreaServiceTest.java

package com.csj2018.o2o.service;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import com.csj2018.o2o.BaseTest;
import com.csj2018.o2o.entity.Area; public class AreaServiceTest extends BaseTest{
@Autowired
private AreaService areaService;
@Test
public void testGetAreaList() {
List<Area> areaList = areaService.getAreaList();
assertEquals("南苑",areaList.get(0).getAreaName());
} }


## 问题:
### 1.AreaServiceTest运行错误
```#log
九月 21, 2019 6:21:41 下午 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2d554825] to prepare test instance [com.csj2018.o2o.service.AreaServiceTest@26abb146]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.csj2018.o2o.service.AreaServiceTest': Unsatisfied dependency expressed through field 'areaService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.csj2018.o2o.service.AreaService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.csj2018.o2o.service.AreaService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
...
```

原因:
1. spring-service.xml文件配置错误 ,修正即可
2. AreaServiceImpl.java是否缺少注释,如Controller注解

校园商铺-2项目设计和框架搭建-9验证Service的更多相关文章

  1. 校园商铺-2项目设计和框架搭建-10验证controller

    1.新建package:com.csj2018.o2o.web.superadmin 2.建立AreaController.java package com.csj2018.o2o.web.super ...

  2. 校园商铺-2项目设计和框架搭建-7验证Dao

    以最简单的地区表为例 1.插入数据 insert into tb_area (area_name, priority) values('东苑', 1),('南苑', 7),('北苑', 5); sel ...

  3. 校园商铺-2项目设计和框架搭建-5配置maven

    /src/main/java 存放业务的Java代码 /src/main/resources 存储项目所用到的资源文件,如各种Spring,batis,日志的配置文件 /src/test/java 单 ...

  4. 校园商铺-2项目设计和框架搭建-8升级mysql驱动相关的配置以支持mysql8

    1.如何升级驱动 1.1步骤: a 确保当前程序能正常访问数据库 b 更新mysql驱动重新运行程序进行校验 maven依赖https://mvnrepository.com/artifact/mys ...

  5. 校园商铺-2项目设计和框架搭建-6逐层完成SSM的各项配置

    1. 创建/src/main/resources/jdbc.properties文件 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://l ...

  6. 项目总结—校园办公管理系统(SSM框架搭建)

    文章目录 CSDN下载地址:校园管理系统 GIT下载地址:校园管理系统 学以致用,学习完SSM框架之后,独立完成一个小院办公管理系统,熟悉框架的开发流程,熟悉项目的开发流程,完成一个简单的校园办公管理 ...

  7. [SSM项目]二-项目设计和框架搭建

    一 10个实体类 选择Integer 而不是int的原因 :当值为空时,int类型会自动为其初始化,这是我们不希望的. 二 配置Maven 目录结构: src/main/java:业务代码 src/m ...

  8. SSM到Spring Boot-从零开发校园商铺平台

    第1章 开发准备 本章包含课程介绍,同时讲解开发网站所需要准备的事情,并且带领大家从零开始搭建一个Maven Web. 1-1 课程导学 1-2 开发准备 第2章 项目设计和框架搭建 本章主要先带领大 ...

  9. SSM到Spring Boot从零开发校园商铺平台

    项目目的 特别 由于准备春招,所以希望各位看客方便的话,能去github上面帮我Star一下项目 https://github.com/Draymonders/Campus-Shop emmm, 已经 ...

随机推荐

  1. <人工智能>人工智能基础

    问题1:扔下圆球的位置(feature特征变量)变化,最终掉落奖项(label结果标签)的变化 feature ----输入 f(x) ----模型,算法 label ----输出 大量已知的数据,训 ...

  2. python 模拟按键模拟键盘按键按下放开

    python模拟按键 pip install pypiwin32安装库 import win32conimport win32apiimport time 导入 打个比方模拟A win32api.ke ...

  3. leetcood学习笔记-112-路径总和

    题目描述: 第一次提交: class Solution(object): def hasPathSum(self, root, sum): """ :type root: ...

  4. delphi常见的错误

    ******************************* * 编 译 错 误 信 息 * ******************************* ';' not allowed befo ...

  5. NX二次开发-UFUN获取工程图视图边界线是否显示UF_DRAW_ask_border_display

    #include <uf.h> #include <uf_draw.h> #include <uf_ui.h> UF_initialize(); logical b ...

  6. 在vs2010下编译boost

    1. 解压缩后,运行bootstrap.bat批处理文件,得到bjam.exe; 2. 在vs2010下编译boost boost最新版本已经支持vs2010,然而直接下载编译会发现一堆bug: 首先 ...

  7. flutter 修饰盒子

    decoration: BoxDecoration( borderRadius: BorderRadius.circular(), //圆角 gradient: RadialGradient( col ...

  8. MDK(KEIL) 两步解决 中文乱码 及 中文光标 半个半个跳的问题

    1. 如果已经用MDK(KEIL)的默认设置写了好多中文,那么先用notepad把文件一一打开然后转变编码格式为 utf-8 without ROM,如下: 2. 如果还没有开始编辑,或者已经用not ...

  9. plugin python was not installed: Cannot download ''

    problem: plugin python was not installed: Cannot download ''........ 1. the first method of resoluti ...

  10. web前端好书推荐 CSS权威指南《第3版,Bootstrap实战,精通CSS 高级Web标准解决方案 第2版 中文

    在我的新博客中==> http://www.suanliutudousi.com/2017/08/24/web%E5%89%8D%E7%AB%AF%E5%A5%BD%E4%B9%A6%E6%8E ...