问题描述
我有一个简单的控制器动作:
I have a simple controller action:
public class CategoriesController
{
@RequestMapping(value = { "/", "" })
public String list(
Model model,
@PageableDefault(size = CategoriesController.PAGE_LIMIT) Pageable pager
)
{
// load page data
Page<Category> page = this.categoryService.findAll(pager);
/* action logic here */
}
}
这是我的 pom.xml
片段:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.6.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
将此添加到我的 applicationContext.xml
后:
After addtion this to my applicationContext.xml
:
<bean class="org.springframework.data.web.config.SpringDataWebConfiguration"/>
我有以下错误:
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 网络附加功能,但由于某种原因它们对我不起作用......手动注册过时的 org.springframework.data.web.PageableArgumentResolver
使其部分工作,但不完全,但我认为这甚至不应该是一个解决方案.
Spring Data itself works fine, JPA repositories are working. But until now I had hand-written pagination in controllers (calculating pages on my own, creating PageRequest
objects by hand). I wanted to make use of Spring Data web extras, but they don't work for me for some reason... registering obsolete org.springframework.data.web.PageableArgumentResolver
by hand partially made it working, but not completely, but still, I don't think this should even be a solution.
在 org.springframework
上启用调试记录器后,我看到:
After enabling debug logger on org.springframework
I see that:
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()
所以它已注册 - 知道为什么它不起作用吗?
So it's registered - any idea why it's not working?
推荐答案
您的问题是您试图混合使用 XML 配置和基于 Java Config 的配置.在这种特殊情况下,这是行不通的.配置类中的 bean 将被实例化,但就是这样,它们没有注册到您的 <mvc:annotation-driven/>
配置中.
Your problem is that you are trying to mix XML configuration and Java Config based configuration. In this particular case that isn't going to work. The bean in the configuration class will be instantiated but that is it, they aren't registered to your <mvc:annotation-driven />
configuration.
您必须手动将 bean 添加到 ConversionService
和您的 RequestMappingHandlerMapping
.无论是我们的开关,至少你的 DispatcherServlet
配置到 Java Config.
You will have to add the beans manually to the ConversionService
and your RequestMappingHandlerMapping
. Either that our switch, at least your DispatcherServlet
configuration to Java Config.
在 XML 中,您可以使用 <mvc:argument-resolvers/>
标记配置其他参数解析器.(这模仿了 SpringDataWebConfiguration
中的配置).
In XML you can configure additional argument-resolvers by using the <mvc:argument-resolvers />
tag. (This mimics the configuration from the SpringDataWebConfiguration
).
<mvc:annotation-driven>
<mvc:argument-resolvers>
<ref bean="sortResolver"/>
<ref bean="pageableResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
<bean id="sortResolver" class="org.springframework.data.web.SortHandlerMethodArgumentResolver" />
<bean id="pageableResolver" class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
<constructor-arg ref="sortResolver" />
</bean>
但是 SpringDataWebConfiguration
做的不仅仅是这两个解析器,它还注册了一个 DomainClassConverter
.如果你也想使用它,你需要一些额外的配置.
However the SpringDataWebConfiguration
does more then only these 2 resolvers it also registers a DomainClassConverter
. If you also want to use that you need some additional configuration.
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<bean class="org.springframework.data.repository.support.DomainClassConverter">
<constructor-arg ref="conversionService" />
</bean>
<mvc:annotation-driven conversion-service="conversionService">
<mvc:argument-resolvers>
<ref bean="sortResolver"/>
<ref bean="pageableResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
<bean id="sortResolver" class="org.springframework.data.web.SortHandlerMethodArgumentResolver" />
<bean id="pageableResolver" class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
<constructor-arg ref="sortResolver" />
</bean>
这篇关于Spring Data 不处理 Pageable 动作参数创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!