你可以用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);
}
}
到目前为止,这对我来说还不错。