基础注解

@Configuration

  • 用于定义配置类,可替换XML配置文件,被注解的类包含一个或多个@Bean
  • 可以被AnnotationConfigApplicationContext或者AnnotationConfigWebApplicationContext 进行扫描

    1. AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    2. ctx.register(AppConfig.class);
    3. ctx.refresh();
    4. MyBean myBean = ctx.getBean(MyBean.class);
  • 关联

    • @Configuation等价于
    • @Bean 等价于
    • @ComponentScan等价于
    • @Component 等价于

      @RestControllerAdvice

  • 对Controller进行增强,全局捕获spring MVC 抛出的异常

  • 自动catch异常,并匹配响应的ExceptionHandler,重新封装。统一返回给前端 ```java @RestControllerAdvice(annotations = RestController.class) public class UniformReponseHandler {

    @ResponseStatus(HttpStatus.OK) public CallResultMsg sendSuccessResponse(){

    1. return new CallResultMsg(true, CodeAndMsg.SUCCESS, null);

    }

@ExceptionHandler(UserDefinedException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public CallResultMsg sendErrorResponse_UserDefined(Exception exception){
    return new CallResultMsg(false, ((UserDefinedException)exception).getException(), null);
}

} ```

数据库

@Mapper

  • 可以替代 @MapperScan(“com.genew.upm.mapper”)

    Web

    @WebFilter

  • Filter的创建和销毁由WEB服务器负责

  • WEB应用启动时,服务器将创建Filter的实例对象,调用init方法