• 校园商铺-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注解

  • 相关阅读:
    Atitit 趋势管理之道 attilax著
    Atitit 循环处理的新特性 for...else...
    Atitit 2017年的技术趋势与未来的大技术趋势
    atitit 用什么样的维度看问题.docx 如何了解 看待xxx
    atitit prj mnrs 项目中的几种经理角色.docx
    Atitit IT办公场所以及度假村以及网点以及租房点建设之道 attilax总结
    Atitit 工具选型的因素与方法 attilax总结
    Atitit.团队文化建设影响组织的的一些原理 法则 定理 效应 p826.v4
    Atiitt 管理方面的误区总结 attilax总结
    Atitit 未来趋势把控的书籍 attilax总结 v3
  • 原文地址:https://www.cnblogs.com/csj2018/p/11564003.html
Copyright © 2020-2023  润新知