第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Java Spring的 JavaConfig 注解

標(biāo)簽:
Java

原文链接

传统spring一般都是基于xml配置的,不过后来新增了许多JavaConfig的注解。特别是springboot,基本都是清一色的java config,不了解一下,还真是不适应。这里备注一下。

@RestController

spring4为了更方便的支持restfull应用的开发,新增了RestController的注解,比Controller注解多的功能就是给底下的RequestMapping方法默认都加上ResponseBody注解,省得自己再去每个去添加该注解。

@Configuration

这个标注该类是spring的配置类,本身自带Component注解

@ImportResource

对应的xml

<import resource="applicationContext-ehcache.xml"/>

存在的必要性

这个是兼容传统xml配置的,毕竟JavaConfig还不是万能的,比如 JavaConfig不能很好地支持aop:advisor和tx:advice , Introduce @EnableAspectJAutoProxy (equivalent to aop:aspectj-autoproxy) , Introduce @Configuration-based equivalent to aop:config XML element

@ComponentScan

对应的xml

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

该配置自动包含了如下配置的功能:

<context:annotation-config/>

就是向Spring容器注册AutowiredAnnotationBeanPostProcessor( 使用@Autowired必须注册 )、CommonAnnotationBeanPostProcessor( 使用@Resource 、@PostConstruct、@PreDestroy等必须注册 )、PersistenceAnnotationBeanPostProcessor( 使用@PersistenceContext必须注册 ) 以及RequiredAnnotationBeanPostProcessor( 使用@Required必须注册 )这4个BeanPostProcessor。

值得注意的是 

@EnableWebMvc

对应的xml如下:

<mvc:annotation-driven />

该配置自动注册DefaultAnnotationHandlerMapping( 来注册handler method和request的mapping关系 )与AnnotationMethodHandlerAdapter( 在实际调用handler method前对其参数进行处理 )两个bean,以支持@Controller注解的使用。

主要的作用如下:

@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开发用,注解返回的http返回码,具体值看org.springframework.http.HttpStatus枚举。一般post方法返回HttpStatus.CREATED,DELETE和PUT方法返回HttpStatus.OK。还可以配置异常处理,见@ExceptionHandler和@ControllerAdvice

@ExceptionHandler

主要用来处理指定的异常,返回返回指定的HTTP状态码,省得每个controller的方法自己去try catch。一般可以为每个应用定义一个异常基类,然后再定义业务异常,这样这里就可以统一捕获业务异常。

    @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的方法调用链产生的异常,如果在spring里头还使用了定时任务啥的,该注解是不会拦截到的。

@ControllerAdvice

配合@ExceptionHandler使用的,用来拦截controller的方法。

@ControllerAdvicepublic 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());
    }
}


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消