• 校园商铺-2项目设计和框架搭建-9验证Service


    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注解

  • 相关阅读:
    如何用php启动exe程序,并在进程中查看?
    如何用原生js或jquery设置select的值
    php5 升级 php7 版本遇到的问题处理方法总结
    phpexcel 读取日期的问题?
    PHPExcel_Reader_Exception: is not recognised as an OLE file in Classes问题解决方法
    QT Unexpected CDB exit 问题的解决办法
    html调用摄像头的方法汇总
    win7 32位 安装opencv-python后,运行时提示 "from .cv2 import *: DLL load failed: 找不到指定的模块" 的解决办法
    centos7 crontab 定时执行python任务不执行的原因及解决办法
    centos 7 生成文件名乱码的问题如何解决?
  • 原文地址:https://www.cnblogs.com/csj2018/p/11564003.html
Copyright © 2020-2023  润新知