Spring3.2 New Annotations @controlleradvice
@ControllerAdvice, a new annotation provided by spring3.2, can be seen in the name of the general meaning of controller enhancement. Let us first look at the implementation of @controlleradvice:
Java code
- @Target (Elementtype.type)
- @Retention (Retentionpolicy.runtime)
- @Documented
- @Component
- Public @interface Controlleradvice {
- }
Nothing special, the annotations use @component annotations, so that when we use <context:component-scan> scanning can also be scanned, specifically, the "12th chapter" 0 Configuration of 12.3 Annotations to implement the bean definition-- Learn Spring3 with me.
Its Javadoc definition is:
Wrote/**
* Indicates the annotated class assists a "Controller".
*
* <p>serves as a specialization of {@link Component @Component}, allowing for
* Implementation classes to be autodetected through classpath scanning.
*
* <p>it is typically used to define {@link Exceptionhandler @ExceptionHandler},
* {@link Initbinder @InitBinder}, and {@link Modelattribute @ModelAttribute}
* methods that apply to all {@link requestmapping @RequestMapping} methods.
*
* @author Rossen Stoyanchev
* @since 3.2
*/
That is, the method of using @exceptionhandler, @InitBinder, @ModelAttribute annotations inside the @controlleradvice annotation is applied to all @RequestMapping annotations. Very simple, but only if the use of @exceptionhandler is most useful, the other two are not very helpful.
Next look at the snippet code:
Java code
- @ControllerAdvice
- Public class Controlleradvicetest {
- @ModelAttribute
- Public User NewUser () {
- System.out.println ("============ applies to all @requestmapping annotation methods and puts the return value into model before it executes");
- return new User ();
- }
- @InitBinder
- public void Initbinder (Webdatabinder binder) {
- System.out.println ("============ applies to all @requestmapping annotation methods, initializes the data binder before it executes");
- }
- @ExceptionHandler (unauthenticatedexception. Class)
- @ResponseStatus (httpstatus.unauthorized)
- Public String processunauthenticatedexception (nativewebrequest request, unauthenticatedexception e) {
- System.out.println ("=========== applies to all @requestmapping annotations, and executes when it throws a Unauthenticatedexception exception");
- return "ViewName"; //Returns a logical view name
- }
- }
If your SPRING-MVC configuration file uses the following methods to scan the bean
Java code
- <context:component-scan base-package="com.sishuok.es" use-default-filters="false" >
- <context:include-filter type="annotation" expression="Org.springframework.stereotype.Controller"/>
- </context:component-scan>
The @ControllerAdvice need to be included, otherwise it will not work:
Java code
- <context:component-scan base-package="com.sishuok.es" use-default-filters="false" >
- <context:include-filter type="annotation" expression="Org.springframework.stereotype.Controller"/>
- <context:include-filter type="annotation" expression=" Org.springframework.web.bind.annotation.ControllerAdvice "/>
- </context:component-scan>
1, the method function of @ModelAttribute annotation Please refer to SPRINGMVC powerful data binding (2)--sixth chapter annotation controller detailed--Follow the "second, exposing form reference object as model data" in the SPRINGMVC, the function is the same, But here is the way all the @requestmapping annotations work. Useful when you need to set global data.
2, @InitBinder annotation of the method function please refer to SPRINGMVC data type conversion-seventh chapter of the annotated controller data validation, type conversion and formatting-followed by the SPRINGMVC, similar to the 1. Useful when global registration is required.
3, @ExceptionHandler, exception handler, the function of this annotation is when the exception of its definition of the method of processing, it can use the data binding provided by SPRINGMVC, such as injection httpservletrequest, etc. You can also accept a Throwable object that is currently thrown. You can refer to the spring annotations of Javadoc or Snowolf to learn Codex (eight) addendum-@ExceptionHandler.
This annotation is very simple, most of the time is actually only @exceptionhandler more useful, the other two uses very few scenes, so that the exception processor can be applied to all controllers, rather than the @controller annotation of a single controller.
Spring3.2 New Annotations @controlleradvice