In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you what the JavaConfig notes on Java Spring are like. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Order
Traditional spring is generally based on xml configuration, but a lot of JavaConfig annotations have been added later. Springboot, in particular, is basically the same java config, do not understand, really do not adapt. Make a note here.
@ RestController
In order to support the development of restfull applications more conveniently, spring4 has added RestController annotations. The more function than Controller annotations is to add ResponseBody annotations to the following RequestMapping methods by default, so that you don't have to add the annotations to each.
@ Configuration
This annotation class is the configuration class of spring and comes with Component annotations.
@ ImportResource
Corresponding xml
The necessity of existence
This is compatible with traditional xml configurations. After all, JavaConfig is not yet *. For example, JavaConfig does not support aop:advisor and tx:advice very well, Introduce @ EnableAspectJAutoProxy (equivalent to aop:aspectj-autoproxy), Introduce @ Configuration-based equivalent to aop:config XML element
@ ComponentScan
Corresponding xml
This configuration automatically includes the following configured features:
It is to register AutowiredAnnotationBeanPostProcessor with the Spring container (you must register for @ Autowired), CommonAnnotationBeanPostProcessor (you must register for @ Resource, @ PostConstruct, @ PreDestroy, etc.), PersistenceAnnotationBeanPostProcessor (you must register for @ PersistenceContext), and RequiredAnnotationBeanPostProcessor (you must register for @ Required).
It is worth noting that the Spring3.1RC2 version does not allow annotated Configuration classes within the package specified by ComponentScan, otherwise an error will be reported.
@ Bean
The corresponding xml is as follows:
@ EnableWebMvc
The corresponding xml is as follows:
This configuration automatically registers DefaultAnnotationHandlerMapping (to register the mapping relationship between handler method and request) and AnnotationMethodHandlerAdapter (to process its parameters before actually calling handler method) to support the use of the @ Controller annotation.
The main functions are as follows:
Configurable ConversionService (to facilitate custom type conversion)
Support for formatting numeric type fields with @ NumberFormat
Support for formatting Date,Calendar and Joda Time fields with @ DateTimeFormat (if classpath has Joda Time)
Parameter verification of @ Valid is supported (if JSR-303-related provider is available in classpath)
Support for XML reading and writing of @ RequestBody/@ResponseBody annotations (if JAXB is in classpath)
Support for JSON reading and writing of @ RequestBody/@ResponseBody annotations (if Jackson is in classpath)
@ ContextConfiguration
Specify java config primarily during junit testing
RunWith (SpringJUnit4ClassRunner.class) @ ContextConfiguration ({"classpath*:spring/*.xml", "classpath:applicationContext.xml", "classpath:applicationContext-rabbitmq.xml", "classpath:applicationContext-mail.xml", "classpath:applicationContext-medis.xml", "classpath:applicationContext-mybatis.xml"}) @ TransactionConfiguration (transactionManager = "mybatisTransactionManager", defaultRollback = false) public class AppBaseTest {/. }
@ ResponseStatus
It is mainly used for rest development. Note the returned http return code. For specific values, see 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, as shown in @ ExceptionHandler and @ ControllerAdvice
@ ExceptionHandler
It is mainly used to handle the specified exception and return the specified HTTP status code, saving each controller method from going to try catch itself. Generally, you can define an exception base class for each application, and then define business exceptions, so that business exceptions can be captured uniformly.
ExceptionHandler (BizException.class) @ ResponseStatus (HttpStatus.BAD_REQUEST) public @ ResponseBody ReturnMessage bizExceptionHandler (Exception ex) {logger.error (ex.getMessage (), ex); return new ReturnMessage (HttpStatus.BAD_REQUEST.value (), ex.getMessage ());}
It is worth noting, however, that this method is limited to exceptions generated by controller's method call chain, and this annotation will not intercept if timing tasks are also used in spring.
@ ControllerAdvice
Method used with @ ExceptionHandler to intercept controller.
@ ControllerAdvice public class ErrorController {private static final Logger logger = LoggerFactory.getLogger (ErrorController.class); @ ExceptionHandler (BizException.class) @ ResponseStatus (HttpStatus.BAD_REQUEST) public @ ResponseBody 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) public @ ResponseBody ReturnMessage serverExceptionHandler (Exception ex) {logger.error (ex.getMessage (), ex); return new ReturnMessage (HttpStatus.INTERNAL_SERVER_ERROR.value (), ex.getMessage ()) }} the above is what Java Spring's JavaConfig notes look like, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.