Howto get rid of mvc:annotation-driven /?
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.
mvc:annotation-driven / with un-annotated controllers
When you add <mvc:annotation-driven />
to your config, it replaces the default set of handler mappings and handler adapters, and those defaults were the ones that handled the old-style controllers.
You have 2 options. First thing to try is to remove <mvc:annotation-driven />
. You can still use annotated controllers without this. It does add extra features like Jackson JSON support, but if you don't need those extra features, then you don't need it. So try your app without <mvc:annotation-driven />
and see if it still works.
Failing that, you can reinstate the mappings and adapters for your old controllers. You didn't say how your controllers used to have their URLs mapped, but try adding these to your config:
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.handler.ControllerClassNameHandlerMapping"/>
If you used SimpleUrlHandlerMapping
, then that should be working already.
You also need to add the HandlerAdapter
back in:
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
Don't just add these in blindly. Try them individually, and see what the minimal set is to get your old controllers working alongside the new ones.
Spring MVC why this Hello World run well without annotation-driven tag (unlike any other project Spring)
<mvc:annotation-driven>
is a helper tag that provides additional services:
- Register the default handler mappings
- Message conversion (for example, converting the object returned in the controller to JSON)
- JSR 303 validation (@Valid annotation)
- Conversion service (if the converter is specified then the controller will be able to take entities as arguments, i.e. the textual ids from request will be converted to the actual business objects with the MVC layer. The same goes for other objects like Dates, Enums etc.)
- Maybe even more features will be added in the new versions of Spring..
it is implemented with AnnotationDrivenBeanDefinitionParser in org.springframework.web.servlet.config package
It's not required for your simple controller to work, if you have appropriate mappings (you can add the required beans explicitly if you want).
But if you want to do some more or less advanced stuff for example JSR 303 validation, implementing a REST service with JSON, registering a conversion service for MVC, this tag turns out to be useful because it keeps the configuration of these services in one place.
That is why it's included into the Spring template project(template project is rather a starting point for developing a Spring application(so you can just go and put @Responsebody
on the values returned by the controller methods or add a converter to MVC) than just an example for the Spring learners).
Also please note that the handler mapping configuration changed in Spring 3.1:
Prior to Spring 3.1, type and method-level request mappings were
examined in two separate stages -- a controller was selected first by
the DefaultAnnotationHandlerMapping and the actual method to invoke
was narrowed down second by the AnnotationMethodHandlerAdapter.
Spring Rest Service - Error with 'mvc:annotation-driven'
Since you can't figure out your mistakes from the first answer I gave, just replace your xsi:schemaLocation with this
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
Adding mvc:annotation-driven / crashes application
This a Java version problem, try to use the latest java 1.8. as the missing Parameter class was added in this release.
For more info see
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Parameter.html
Or downgrade the spring version :)
MVC Annotation Spring MVC mvc:annotation-driven /
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
. But, mvc:annotation-driven does some extra job on configuring the special beans that would not have been configured if you are not using this element in your XML file.
This tag would registers the HandlerMapping and HandlerAdapter required to dispatch requests to your @Controllers. In addition, it also applies some defaults based on what is present in your classpath. Such defaults are:
- Using the Spring 3 Type ConversionService as a simpler and more
robust alternative to JavaBeans PropertyEditors Support for formatting Number fields with @NumberFormat
Support for formatting Date, Calendar, and Joda Time fields with
@DateTimeFormat, if Joda Time is on the classpath- Support for validating @Controller inputs with @Valid, if a JSR-303
Provider is on the classpath - Support for reading and writing XML, if JAXB is on the classpath
- Support for reading and writing JSON, if Jackson is on the classpath
context:component-scan element in the spring configuration file would eliminate the need for declaring all the beans in the XML files. Look at the below declaration in your spring configuration file:
<context:component-scan base-package="org.controller"/>
The above declaration in the spring application configuration file would scan the classes inside the specified package and create the beans instance. Note that it could create beans only if that class is annotated with correct annotations. The following are the annotations scanned by this element:
- @Component
- @Repository
- @Service
- @Controller
Related Topics
Nosuchmethoderror on Startup in Java Jersey App
Java Creating a New Objectinputstream Blocks
How to Find the Source Code for Java's Square Root Function
Is There a Java Equivalent to C#'s 'Yield' Keyword
Java: Calling a Super Method Which Calls an Overridden Method
How to Set Jvm Arguments in Intellij Idea
How to Use Selenium Webdriver on Local Webpage (On My Pc) Instead of One Located Somewhere Online
Not Implementing All of the Methods of Interface. Is It Possible
On-The-Fly, In-Memory Java Code Compilation for Java 5 and Java 6
How to Convert Double[] to Double[]
Must Issue a Starttls Command First
Where Is the Correct Location to Put Log4J.Properties in an Eclipse Project