Java 如何摆脱 <mvc:annotation-driven />?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3693397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Howto get rid of <mvc:annotation-driven />?
提问by Ta Sas
Up to now, <mvc:annotation-driven />
has caused plenty of trouble for me, so I would like to get rid of it. Although the spring framework docs clearly say what it is supposed to be doing, a listing of tags actually summar <mvc:annotation-driven />
is lacking.
到现在<mvc:annotation-driven />
为止,给我带来了很多麻烦,所以我想摆脱它。尽管spring 框架文档清楚地说明了它应该做什么<mvc:annotation-driven />
,但缺少实际总结的标签列表。
So I'm stuck with removing <mvc:annotation-driven />
and now getting the error
所以我坚持删除<mvc:annotation-driven />
,现在收到错误
WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/webapp/trainees] in DispatcherServlet with name 'workoutsensor'
WARN osweb.servlet.PageNotFound - 在 DispatcherServlet 中找不到名称为“workoutsensor”的带有 URI [/webapp/trainees] 的 HTTP 请求的映射
for all Urls supposed to be resolved by the controller classes (in this case: ./trainees
). Any suggestion where I can read more about <mvc:annotation-driven />
? I desperately would like to know what tags exactly are represented by <mvc:annotation-driven />
.
对于所有应该由控制器类解析的 URL(在这种情况下:)./trainees
。有什么建议可以让我阅读更多内容<mvc:annotation-driven />
吗?我非常想知道<mvc:annotation-driven />
.
采纳答案by Bozho
You can use BeanPostProcessor
to customize each bean defined by <mvc:annotation-driven />
. The javadocs now details all beans the tag registers.
您可以使用BeanPostProcessor
自定义由<mvc:annotation-driven />
. javadocs 现在详细说明了标记注册的所有 bean。
If you really want to get rid of it, you can look at the source code of org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
如果你真的想摆脱它,你可以看一下的源代码 org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
And you can see which beans it is defining. I've done this 'exercise' (not for all of them, but for those I need), so here are they:
你可以看到它定义了哪些 bean。我已经完成了这项“锻炼”(不是针对所有人,而是针对我需要的那些人),以下是它们:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.yourpackage.web.util.CommonWebBindingInitializer" />
</property>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.feed.AtomFeedHttpMessageConverter" />
<bean class="org.springframework.http.converter.feed.RssChannelHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
<!-- bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" /-->
</list>
</property>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
Now, above you see the CommonWebBindingInitializer
. You have to create this class, in order to use conversion and validation:
现在,您可以在上方看到CommonWebBindingInitializer
. 您必须创建此类,才能使用转换和验证:
public class CommonWebBindingInitializer implements WebBindingInitializer {
@Autowired
private Validator validator;
@Autowired
private ConversionService conversionService;
@Override
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.setValidator(validator);
binder.setConversionService(conversionService);
}
}
And this works fine for me so far. Feel free to report any problems with it.
到目前为止,这对我来说很好用。随时报告任何问题。
回答by earldouglas
If you want to avoid the mvc:annotation-driven
tag, you can simply create DefaultAnnotationHandlerMapping
and AnnotationMethodHandlerAdapter
beans yourself, but it sounds like it would be better to get to the root of your troubles with the tag itself.
如果你想避免使用mvc:annotation-driven
标签,你可以简单地自己创建DefaultAnnotationHandlerMapping
和AnnotationMethodHandlerAdapter
bean,但听起来最好用标签本身找到问题的根源。
What are the symptoms of your problem? What are you trying to do with your Spring MVC application?
你的问题的症状是什么?你想用你的 Spring MVC 应用程序做什么?
If you want to know what's going on under the covers when you use mvc:annotation-driven, see the AnnotationDrivenBeanDefinitionParser
.parse()
method.
如果您想知道在使用 mvc:annotation-driven 时发生了什么,请参阅方法。AnnotationDrivenBeanDefinitionParser
.parse()
回答by sbk
Old question I know, but this may help someone. Thanks to posts on this page and also over here, I've used the following to replace the annotation-driven tag in Roo 1.2 app. They kicker for me was needing support type conversion in roo app list view.
我知道老问题,但这可能对某人有所帮助。感谢本页和此处的帖子,我使用以下内容替换了 Roo 1.2 应用程序中的注释驱动标签。他们对我来说更重要的是需要在 roo 应用程序列表视图中支持类型转换。
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<bean id="conversionServiceExposingInterceptor"
class="org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor">
<constructor-arg ref="conversionService" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="order" value="0" />
<property name="interceptors">
<list>
<ref bean="conversionServiceExposingInterceptor" />
</list>
</property>
</bean>
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
<property name="validator" ref="validator" />
</bean>
</property>
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean
class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
<bean
class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
</list>
</property>
</bean>
回答by bmeurant
While overriding, be carreful to consider also custom Execution management overriding. Otherwise, all your custom Exception mappings will fail. You will have to reuse messageCoverters with a list bean :
在覆盖时,还要注意考虑自定义执行管理覆盖。否则,您所有的自定义异常映射都将失败。您将不得不使用列表 bean 重用 messageCoverters:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<util:list id="messageConverters">
<bean class="your.custom.message.converter.IfAny"></bean>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter"></bean>
</util:list>
<bean name="exceptionHandlerExceptionResolver"
class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
<property name="order" value="0"/>
<property name="messageConverters" ref="messageConverters"/>
</bean>
<bean name="handlerAdapter"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
<property name="validator" ref="validator" />
</bean>
</property>
<property name="messageConverters" ref="messageConverters"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>