java - SSH框架中写分页时service层中不能注入分页类
问题描述
service层
@Servicepublic class AuthorAdminServiceImpl implements IAuthorAdminService { @Resource private IAuthorAdminDao iauthoradmindao; @Resource PageUntil<AuthorAdmin> pageUntil;@Override public PageUntil<AuthorAdmin> findByPage(int currentPage) {pageUntil.setCurrentPage(currentPage);// 当前多少页int pageSize = 3;pageUntil.setPagesize(pageSize);// 最多显示多少记录int totalCount = iauthoradmindao.findCount();// 数据库中所有的记录pageUntil.setTotalCount(totalCount);double tc = totalCount;Double num = Math.ceil(tc / pageSize);// 页面显示的总页数=数据库的总记录数/最多显示多少记录pageUntil.setTotalPage(num.intValue());int begin = (currentPage - 1) * pageSize;// 当前页开头位置的索引List<AuthorAdmin> list = iauthoradmindao.findByPage(begin, pageSize);if(list==null){ System.out.println('list findByPage service error');}pageUntil.setList(list);return pageUntil; }
action层
public String listAuthorAdmin() {PageUntil<AuthorAdmin> pageuntil = iauthoradminservice.findByPage(currentPage);ActionContext.getContext().getValueStack().push(pageuntil);return 'listSuccess'; }
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ’authorAdminAction’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ’authorAdminServiceImpl’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.lcy.until.PageUntil com.lcy.service.author.admin.AuthorAdminServiceImpl.pageUntil; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.lcy.until.PageUntil] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}----------Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ’authorAdminServiceImpl’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.lcy.until.PageUntil com.lcy.service.author.admin.AuthorAdminServiceImpl.pageUntil; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.lcy.until.PageUntil] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}----------Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.lcy.until.PageUntil com.lcy.service.author.admin.AuthorAdminServiceImpl.pageUntil; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.lcy.until.PageUntil] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}----------Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.lcy.until.PageUntil] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
如果不是使用注入而使用new的方法就行,请问为什么?
问题解答
回答1:PageUntil这个类也是要用@Component
回答2:你没有正确使用@Resource,导致配置文件里面找不到名称属性为'iauthoradmindao'的<bean>,故而注入失败!你可以将@Resource 更换成 @AutoWired 试试看。这两个注解的注入方式是不一样的,当然前提是你的xml文件中要配有'IAuthorAdminDao '类型的<bean>
详情参考:https://www.zhihu.com/questio...
回答3:我觉得 new出来最简单了。你这个类需要考虑到特殊的问题吗?
相关文章:
1. Android明明可以直接分享,为什么还要用微信开放平台、微博开放平台的sdk?2. javascript - 单页面应用怎么监听ios微信返回键?3. angular.js - 在ionic下,利用javascript导入百度地图,pc端可以显示,移动端无法显示4. nginx - 关于javaweb项目瘦身问题,前期开发后,发现项目占用存贮空间太大,差不多1.2个G,怎么实现瘦身,动态页面主要是jsp。5. css3 - 求教个问题,关于响应式布局,跟ipad有关,媒体查询失效?6. javascript - 如何保证几个ajax提交成功;7. css - 浏览器缩放分辨率为什么布局会变8. angular.js - 百度支持_escaped_fragment_吗?9. vue.js - vue apache 代理设置10. 我在centos容器里安装docker,也就是在容器里安装容器,报错了?
