Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto get rid of <mvc:annotation-driven />?

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.

So I'm stuck with removing <mvc:annotation-driven /> and now getting the error

WARN o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/webapp/trainees] in DispatcherServlet with name 'workoutsensor'

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 />.

like image 754
Ta Sas Avatar asked Sep 12 '10 01:09

Ta Sas


People also ask

What is mvc annotation-driven />?

mvc:annotation-driven is a tag added in Spring 3.0 which does the following: Configures the Spring 3 Type ConversionService (alternative to PropertyEditors) Adds support for formatting Number fields with @NumberFormat.

What is the use of mvc annotation-driven /> in spring?

mvc:annotation-driven is used for enabling the Spring MVC components with its default configurations. If you dont include mvc:annotation-driven also your MVC application would work if you have used the context:component-scan for creating the beans or defined the beans in your XML file.

What is the use of context annotation config?

<context:annotation-config> is used to activate annotations in beans already registered in the application context (no matter if they were defined with XML or by package scanning).


2 Answers

You can use BeanPostProcessor to customize each bean defined by <mvc:annotation-driven />. The javadocs now details all beans the tag registers.

If you really want to get rid of it, you can look at the source code of 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 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">

Now, above you see the CommonWebBindingInitializer. You have to create this class, in order to use conversion and validation:

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.

like image 194
Bozho Avatar answered Oct 18 '22 07:10

Bozho


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.

What are the symptoms of your problem? What are you trying to do with your Spring MVC application?

If you want to know what's going on under the covers when you use mvc:annotation-driven, see the AnnotationDrivenBeanDefinitionParser.parse() method.

like image 31
earldouglas Avatar answered Oct 18 '22 09:10

earldouglas