当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Spring中的JavaConfig注解

详解Spring中的JavaConfig注解

2019年07月22日  | 移动技术网IT编程  | 我要评论

前言

大家都知道传统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。

值得注意的是 spring3.1rc2版本 是不允许注解configuration的类在componentscan指定的包范围内的,否则会报错。

@bean

对应的xml如下:

<bean id="objectmapper" class="org.codehaus.jackson.map.objectmapper" />

@enablewebmvc

对应的xml如下:

<mvc:annotation-driven />

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

主要的作用如下:

  1. 可配置的conversionservice(方便进行自定义类型转换)
  2. 支持用@numberformat格式化数字类型字段
  3. 支持用@datetimeformat格式化date,calendar以及joda time字段( 如果classpath有joda time的话 )
  4. 支持@valid的参数校验( 如果jsr-303相关provider有在classpath的话 )
  5. 支持@requestbody/@responsebody注解的xml读写( 如果jaxb在classpath的话 )
  6. 支持@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开发用,注解返回的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的方法。

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网