zoukankan      html  css  js  c++  java
  • 校园商铺-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注解

  • 相关阅读:
    Java实现蓝桥杯七对数字
    Java实现蓝桥杯七对数字
    ubuntu下交叉编译windows c程序
    Linux下开发Windows平台运行的程序
    Fedora 11中用MinGW编译Windows的Qt4程序(在Linux系统下编译Windows的程序)
    C++内存问题大集合(指针问题,以及字符串拷贝问题,确实挺危险的)
    C/C++的编译器|编译环境(非常全面的比较)
    关于 js 中的 call 和 apply使用理解
    看我如何应对业务需求变化,领域模型调整?
    MVC 用扩展方法执行自定义视图,替代 UIHint
  • 原文地址:https://www.cnblogs.com/csj2018/p/11564003.html
Copyright © 2011-2022 走看看