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. python - django 里自定義的 login 方法,如何使用 login_required()2. python如何不改動文件的情況下修改文件的 修改日期3. angular.js - Angular路由和express路由的組合使用問題4. angular.js - angularjs 用ng-reapt渲染的dom 怎么獲取上面的屬性5. mysql優化 - mysql count(id)查詢速度如何優化?6. 主從備份 - 跪求mysql 高可用主從方案7. css3 - [CSS] 動畫效果 3D翻轉bug8. angular.js - 不適用其他構建工具,怎么搭建angular1項目9. mysql主從 - 請教下mysql 主動-被動模式的雙主配置 和 主從配置在應用上有什么區別?10. node.js - node_moduls太多了
![css3 - [CSS] 動畫效果 3D翻轉bug](http://www.cgvv.com.cn/attached/image/news/202304/110831f073.png)