这是单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class mvc {
//@Resource(name="itemServiceMapper")
@Autowired
private ItemService ItemService;
@Test
public void testmapper(){
System.out.println(ItemService.getItemList());
}
}
这是单元测试的运行结果,
但是我在controller里注入却报错...
@Controller
public class ItemController {
//@Resource(name="ItemServiceMapper")
@Autowired
private ItemService ItemService;
@RequestMapping("itemList")
public ModelAndView showItemList()
{
ModelAndView mv=new ModelAndView();
List<Item> list = ItemService.getItemList();
mv.addObject("itemList", list);
mv.setViewName("itemList");
return mv;
}
报错
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'itemController': Unsatisfied dependency expressed through field 'ItemService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'service.ItemService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
讲道理单元测试都成功了,那些配置应该没有错才对啊...
@Service注解也加了,Controller,ServiceImpl包也扫描了...