Spring Data不处理Pageable action参数创建
我有一个简单的控制器动作:
public class CategoriesController { @RequestMapping(value = { "/", "" }) public String list( Model model, @PageableDefault(size = CategoriesController.PAGE_LIMIT) Pageable pager ) { // load page data Page page = this.categoryService.findAll(pager); /* action logic here */ } }
这是我的pom.xml
片段:
org.springframework spring-beans 3.2.4.RELEASE org.springframework spring-web 3.2.4.RELEASE org.springframework spring-webmvc 3.2.4.RELEASE org.springframework.data spring-data-commons 1.6.4.RELEASE org.springframework.data spring-data-jpa 1.5.0.RELEASE
将此添加到我的applicationContext.xml
:
我有以下错误:
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.domain.Pageable]: Specified class is an interface
Spring Data本身运行正常,JPA存储库正在运行。 但到目前为止,我在控制器中手写了分页(我自己计算页面,手动创建PageRequest
对象)。 我想利用Spring Data网络附加function,但由于某些原因它们对我不起作用…手动注册过时的org.springframework.data.web.PageableArgumentResolver
部分使其工作,但不完全,但仍然,我认为这甚至不应该是一个解决方案。
在org.springframework
上启用调试记录器后,我看到:
01:37:33.850 [localhost-startStop-1] DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader - Registering bean definition for @Bean method org.springframework.data.web.config.SpringDataWebConfiguration.pageableResolver()
所以它已注册 – 任何想法为什么它不起作用?
您的问题是您正在尝试混合XML配置和基于Java Config的配置。 在这种特殊情况下,这是行不通的。 配置类中的bean将被实例化,但也就是说,它们未注册到
配置中。
您必须手动将Bean添加到ConversionService
和RequestMappingHandlerMapping
。 要么我们的开关,至少你的DispatcherServlet
配置为Java Config。
在XML中,您可以使用
标记配置其他参数解析器。 (这模仿SpringDataWebConfiguration的配置)。
但是, DomainClassConverter
只执行了这两个解析器,它还注册了一个DomainClassConverter
。 如果您还想使用它,则需要一些额外的配置。
或者您可以将其添加到用户ApplicationContext
它将适用于Spring 4