新聞中心
序

10年積累的成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有黎平免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
傳統(tǒng)spring一般都是基于xml配置的,不過后來新增了許多JavaConfig的注解。特別是springboot,基本都是清一色的java config,不了解一下,還真是不適應(yīng)。這里備注一下。
@RestController
spring4為了更方便的支持restfull應(yīng)用的開發(fā),新增了RestController的注解,比Controller注解多的功能就是給底下的RequestMapping方法默認(rèn)都加上ResponseBody注解,省得自己再去每個去添加該注解。
@Configuration
這個標(biāo)注該類是spring的配置類,本身自帶Component注解
@ImportResource
對應(yīng)的xml
存在的必要性
這個是兼容傳統(tǒng)xml配置的,畢竟JavaConfig還不是***的,比如 JavaConfig不能很好地支持aop:advisor和tx:advice , Introduce @EnableAspectJAutoProxy (equivalent to aop:aspectj-autoproxy) , Introduce @Configuration-based equivalent to aop:config XML element
@ComponentScan
對應(yīng)的xml
該配置自動包含了如下配置的功能:
就是向Spring容器注冊AutowiredAnnotationBeanPostProcessor( 使用@Autowired必須注冊 )、CommonAnnotationBeanPostProcessor( 使用@Resource 、@PostConstruct、@PreDestroy等必須注冊 )、PersistenceAnnotationBeanPostProcessor( 使用@PersistenceContext必須注冊 ) 以及RequiredAnnotationBeanPostProcessor( 使用@Required必須注冊 )這4個BeanPostProcessor。
值得注意的是 Spring3.1RC2版本 是不允許注解Configuration的類在ComponentScan指定的包范圍內(nèi)的,否則會報(bào)錯。
@Bean
對應(yīng)的xml如下:
@EnableWebMvc
對應(yīng)的xml如下:
該配置自動注冊DefaultAnnotationHandlerMapping( 來注冊handler method和request的mapping關(guān)系 )與AnnotationMethodHandlerAdapter( 在實(shí)際調(diào)用handler method前對其參數(shù)進(jìn)行處理 )兩個bean,以支持@Controller注解的使用。
主要的作用如下:
-
可配置的ConversionService(方便進(jìn)行自定義類型轉(zhuǎn)換)
-
支持用@NumberFormat格式化數(shù)字類型字段
-
支持用@DateTimeFormat格式化Date,Calendar以及Joda Time字段( 如果classpath有Joda Time的話 )
-
支持@Valid的參數(shù)校驗(yàn)( 如果JSR-303相關(guān)provider有在classpath的話 )
-
支持@RequestBody/@ResponseBody注解的XML讀寫( 如果JAXB在classpath的話 )
-
支持@RequestBody/@ResponseBody注解的JSON讀寫( 如果Jackson在classpath的話 )
@ContextConfiguration
主要在junit測試時指定java config
- @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
主要是rest開發(fā)用,注解返回的http返回碼,具體值看org.springframework.http.HttpStatus枚舉。一般 post方法返回HttpStatus.CREATED,DELETE和PUT方法返回HttpStatus.OK。還可以配置異常處理,見 @ExceptionHandler和@ControllerAdvice
@ExceptionHandler
主要用來處理指定的異常,返回返回指定的HTTP狀態(tài)碼,省得每個controller的方法自己去try catch。一般可以為每個應(yīng)用定義一個異?;?,然后再定義業(yè)務(wù)異常,這樣這里就可以統(tǒng)一捕獲業(yè)務(wù)異常。
- @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());
- }
不過值得注意的是這種方法僅限于controller的方法調(diào)用鏈產(chǎn)生的異常,如果在spring里頭還使用了定時任務(wù)啥的,該注解是不會攔截到的。
@ControllerAdvice
配合@ExceptionHandler使用的,用來攔截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());
- }
- }
網(wǎng)頁標(biāo)題:詳細(xì)解說JavaSpring的JavaConfig注解
本文鏈接:http://m.fisionsoft.com.cn/article/dhgeoop.html


咨詢
建站咨詢
