Spring3.2 New Annotations @controlleradvice

Source: Internet
Author: User

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
    1. @Target (Elementtype.type)
    2. @Retention (Retentionpolicy.runtime)
    3. @Documented
    4. @Component
    5. Public @interface Controlleradvice {
    6. }

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
  1. @ControllerAdvice
  2. Public class Controlleradvicetest {
  3. @ModelAttribute
  4. Public User NewUser () {
  5. System.out.println ("============ applies to all @requestmapping annotation methods and puts the return value into model before it executes");
  6. return new User ();
  7. }
  8. @InitBinder
  9. public void Initbinder (Webdatabinder binder) {
  10. System.out.println ("============ applies to all @requestmapping annotation methods, initializes the data binder before it executes");
  11. }
  12. @ExceptionHandler (unauthenticatedexception. Class)
  13. @ResponseStatus (httpstatus.unauthorized)
  14. Public String processunauthenticatedexception (nativewebrequest request, unauthenticatedexception e) {
  15. System.out.println ("=========== applies to all @requestmapping annotations, and executes when it throws a Unauthenticatedexception exception");
  16. return "ViewName"; //Returns a logical view name
  17. }
  18. }

If your SPRING-MVC configuration file uses the following methods to scan the bean

Java code
    1. <context:component-scan base-package="com.sishuok.es" use-default-filters="false" >
    2. <context:include-filter type="annotation" expression="Org.springframework.stereotype.Controller"/>
    3. </context:component-scan>

The @ControllerAdvice need to be included, otherwise it will not work:

Java code
    1. <context:component-scan base-package="com.sishuok.es" use-default-filters="false" >
    2. <context:include-filter type="annotation" expression="Org.springframework.stereotype.Controller"/>
    3. <context:include-filter type="annotation" expression=" Org.springframework.web.bind.annotation.ControllerAdvice "/>
    4. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.