日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳細解說JavaSpring的JavaConfig注解

10年積累的成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有黎平免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

傳統(tǒng)spring一般都是基于xml配置的,不過后來新增了許多JavaConfig的注解。特別是springboot,基本都是清一色的java config,不了解一下,還真是不適應(yīng)。這里備注一下。

@RestController

spring4為了更方便的支持restfull應(yīng)用的開發(fā),新增了RestController的注解,比Controller注解多的功能就是給底下的RequestMapping方法默認都加上ResponseBody注解,省得自己再去每個去添加該注解。

@Configuration

這個標注該類是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)的,否則會報錯。

@Bean

對應(yīng)的xml如下:

  
 

@EnableWebMvc

對應(yīng)的xml如下:

該配置自動注冊DefaultAnnotationHandlerMapping( 來注冊handler method和request的mapping關(guān)系 )與AnnotationMethodHandlerAdapter( 在實際調(diào)用handler method前對其參數(shù)進行處理 )兩個bean,以支持@Controller注解的使用。

主要的作用如下:

  • 可配置的ConversionService(方便進行自定義類型轉(zhuǎn)換)

  • 支持用@NumberFormat格式化數(shù)字類型字段

  • 支持用@DateTimeFormat格式化Date,Calendar以及Joda Time字段( 如果classpath有Joda Time的話 )

  • 支持@Valid的參數(shù)校驗( 如果JSR-303相關(guān)provider有在classpath的話 )

  • 支持@RequestBody/@ResponseBody注解的XML讀寫( 如果JAXB在classpath的話 )

  • 支持@RequestBody/@ResponseBody注解的JSON讀寫( 如果Jackson在classpath的話 )

@ContextConfiguration

主要在junit測試時指定java config

  
 
  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration({
  3.     "classpath*:spring/*.xml",
  4.     "classpath:applicationContext.xml",
  5.     "classpath:applicationContext-rabbitmq.xml",
  6.     "classpath:applicationContext-mail.xml",
  7.     "classpath:applicationContext-medis.xml",
  8.     "classpath:applicationContext-mybatis.xml"})
  9. @TransactionConfiguration(transactionManager = "mybatisTransactionManager", defaultRollback = false)
  10. public class AppBaseTest {
  11.    //......
  12. }

@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ù)異常。

  
 
  1. @ExceptionHandler(BizException.class)
  2.  @ResponseStatus(HttpStatus.BAD_REQUEST)
  3.  public @ResponseBody
  4.  ReturnMessage bizExceptionHandler(Exception ex) {
  5.      logger.error(ex.getMessage(),ex);
  6.      return new ReturnMessage(HttpStatus.BAD_REQUEST.value(),ex.getMessage());
  7.  }

不過值得注意的是這種方法僅限于controller的方法調(diào)用鏈產(chǎn)生的異常,如果在spring里頭還使用了定時任務(wù)啥的,該注解是不會攔截到的。

@ControllerAdvice

配合@ExceptionHandler使用的,用來攔截controller的方法。

  
 
  1. @ControllerAdvice
  2. public class ErrorController {
  3.     private static final Logger logger = LoggerFactory.getLogger(ErrorController.class);
  4.     @ExceptionHandler(BizException.class)
  5.     @ResponseStatus(HttpStatus.BAD_REQUEST)
  6.     public @ResponseBody
  7.     ReturnMessage bizExceptionHandler(Exception ex) {
  8.         logger.error(ex.getMessage(),ex);
  9.         return new ReturnMessage(HttpStatus.BAD_REQUEST.value(),ex.getMessage());
  10.     }
  11.     @ExceptionHandler(Exception.class)
  12.     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  13.     public @ResponseBody
  14.     ReturnMessage serverExceptionHandler(Exception ex) {
  15.         logger.error(ex.getMessage(),ex);
  16.         return new ReturnMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(),ex.getMessage());
  17.     }
  18. }

網(wǎng)頁標題:詳細解說JavaSpring的JavaConfig注解
網(wǎng)站路徑:http://m.5511xx.com/article/dhgeoop.html