Spring4 New Javaconfig Annotations

Source: Internet
Author: User
Tags getmessage try catch

1. @RestController

Spring4 in order to more easily support the development of restfull applications, added Restcontroller annotations, more than the controller annotation function is to the bottom of the Requestmapping method by default, plus responsebody annotations, Save yourself from going to each other to add the note.

2. @Configuration

This callout class is spring's configuration class (IOC container), which itself comes with component annotations

3. @ImportResource

The corresponding XML
<import resource= "Applicationcontext-ehcache.xml"/>

4. @ComponentScan

The corresponding XML

<context:component-scan base-package= "Com.xixicat.app"/>

This configuration automatically includes the following configuration features:

<context:annotation-config/>

is to register 4 beanpostprocessor with a spring container

    • Autowiredannotationbeanpostprocessor (must be registered with @autowired)
    • Commonannotationbeanpostprocessor (must be registered with @resource, @PostConstruct, @PreDestroy, etc.)
    • Persistenceannotationbeanpostprocessor (must be registered with @persistencecontext)
    • Requiredannotationbeanpostprocessor (must be registered with @required)
5. @Bean

The corresponding XML is as follows:

<bean id= "Objectmapper" class= "Org.codehaus.jackson.map.ObjectMapper"/>
6. @EnableWebMvc

The corresponding XML is as follows:

<mvc:annotation-driven/>

This configuration automatically registers two beans to support the use of @controller annotations

    • Defaultannotationhandlermapping (to register the mapping relationship of handler method and request)
    • Annotationmethodhandleradapter (the arguments are processed before the handler method is actually called).

The main functions are as follows:

    • Configurable Conversionservice (for easy custom type conversions)
    • Support for formatting numeric type fields with @numberformat
    • Support for formatting Date,calendar and Joda Time fields with @datetimeformat (if Classpath has Joda time)
    • Supports @valid parameter checking (if JSR-303 related provider is in classpath)
    • Supports @requestbody/@ResponseBody annotations for XML reading and writing (if JAXB is in classpath)
    • JSON read/write support for @requestbody/@ResponseBody annotations (if Jackson is in classpath)
7. @ContextConfiguration

Specifying Java config primarily during junit testing

@RunWith (Springjunit4classrunner.class) @ContextConfiguration ({"Classpath*:spring/*.xml", "Classpath: Applicationcontext.xml "}) @TransactionConfiguration (TransactionManager =" Mybatistransactionmanager ", Defaultrollback = false) public class Appbasetest {//...}
8. @ResponseStatus

Mainly for rest development, the annotations return the HTTP return code, the exact value of the Org.springframework.http.HttpStatus enumeration. The General Post method returns Httpstatus.created,delete and the Put method returns Httpstatus.ok. You can also configure exception handling, see @ExceptionHandler and @controlleradvice

9. @ExceptionHandler

It is mainly used to handle the specified exception, return the specified HTTP status code, and save each controller's way to try catch itself. You can typically define an exception base class for each application, and then define a business exception so that the business exception can be captured uniformly here.

However, it is important to note that this method is limited to the exception generated by the controller's method call chain, which is not intercepted if a timed task is used in spring.

10. @ControllerAdvice

A method used in conjunction with @exceptionhandler to intercept a controller.

@ControllerAdvice public class Errorcontroller {private static final Logger Logger = Loggerfactory.getlogger (Errorcontro Ller.class); @ExceptionHandler (Bizexception.class) @ResponseStatus (httpstatus.bad_request) @ResponseBodypublic ReturnMessage Bizexceptionhandler (Exception ex) {Logger.error (Ex.getmessage (), ex); return new ReturnMessage (Httpstatus.bad_ Request.value (), Ex.getmessage ()); }} @ExceptionHandler (Exception.class) @ResponseStatus (httpstatus.internal_server_error) @ResponseBody public ReturnMessage Serverexceptionhandler (Exception ex) {Logger.error (Ex.getmessage (), ex); return new ReturnMessage ( HttpStatus.INTERNAL_SERVER_ERROR.value (), Ex.getmessage ()); }

  

Spring4 New Javaconfig Annotations

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.