您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何摆脱<mvc:annotation-driven />?

如何摆脱<mvc:annotation-driven />?

你可以用BeanPostProcessor自定义定义的每个bean <mvc:annotation-driven />。现在,javadocs详细说明了标签注册的所有bean。

如果你真的想摆脱它,可以查看的源代码org.springframework.web.servlet.config.AnnotationDrivenBeanDeFinitionParser

你会看到它正在定义哪个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.MappingJacksonHttpMessageConverter" />
                <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">

现在,在上方你会看到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);
    }

}

到目前为止,这对我来说还不错。

其他 2022/1/1 18:17:52 有450人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶