是这样的, 配置好了所有的配置文件之后, 用junit测试, 测试代码如下:
public class CategoryServiceImplTest { private ApplicationContext actx; @Before public void setUpBeforeClass() throws Exception { actx = new ClassPathXmlApplicationContext(new String[] { "classpath:spring/applicationContext-dao.xml", "classpath:spring/applicationContext-service.xml", "classpath:spring/applicationContext-transactional.xml", "classpath:spring/applicationContext-mvc.xml"}); } @Test public void test() { CategoryController controller = (CategoryController) actx.getBean("categoryController"); String str = (String) controller.queryCList2().getViewName(); System.out.println(str); } }
我也分别获取了categoryMapper的bean和categoryService的bean.
其中categoryService中注入了categoryMapper,
controller中注入了categoryService.
controller类的代码:
@Controller public class CategoryController { @Resource private CategoryService categoryService; @RequestMapping("/queryCList2") public ModelAndView queryCList2() { ModelAndView mv = new ModelAndView(); //categoryService = (CategoryService) actx.getBean("categoryService"); List<Category> cList = categoryService.findAll(); mv.addObject("cList", cList); mv.setViewName("index"); return mv; } }
然后, 我发布到tomcat, 就报错.
1.Error creating bean with name 'categoryController': //上面的类注入了下面的类 2.Error creating bean with name 'categoryService': 3.No qualifying bean of type [com.shop.mapping.CategoryMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER)}
@Service("categoryService") public class CategoryServiceImpl implements CategoryService { @Resource private CategoryMapper categoryMapper;
No qualifying bean of type [com.shop.mapping.CategoryService]...
请问这是什么原因呢,谢谢您的解答.
看样子是mybatis的mapper类没有生成造成的,也没有说mapper生成错误,可能是mybatis的基本配置就有问题,根本没有去创建Mapper
1.检查一下配置文件的位置路径有没有问题
<spanstyle="font-size:13.3333px;">2.mybatis扫描路径配置问题
3.Mapper类注解是否添加
写个junit测试看下CategoryMapper是否能够取到怀疑<spanstyle="font-family:'MicrosoftYaHei',Verdana,sans-serif,宋体;font-size:14px;line-height:normal;background-color:#FFFFFF;">Mapper类注解是否正确回复<aclass='referer'target='_blank'>@丶KKdo:可以看一下楼下的.已经获取过了,也没有问题,使用的是自动扫描包的形式配置的mapperbean.<imgsrc="http://static.oschina.net/uploads/space/2016/0822/090524_940i_2861620.png"alt="">
<preclass="brush:java;toolbar:true;auto-links:false;">privateApplicationContextactx;@BeforepublicvoidsetUpBeforeClass()throwsException{actx=newClassPathXmlApplicationContext(newString[]{"classpath:spring/applicationContext-dao.xml","classpath:spring/applicationContext-service.xml","classpath:spring/applicationContext-transactional.xml"});}@Testpublicvoidtest(){CategoryMappercategoryMapper=(CategoryMapper)actx.getBean("categoryMapper");CategoryServicecategoryService=(CategoryService)actx.getBean("categoryService");System.out.println(categoryMapper);System.out.println(categoryService);CategoryControllercontroller=(CategoryController)actx.getBean("categoryController");Stringstr=(String)controller.queryCList2().getViewName();System.out.println(str);}版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。