基础注解
@Configuration
- 用于定义配置类,可替换XML配置文件,被注解的类包含一个或多个@Bean
可以被AnnotationConfigApplicationContext或者AnnotationConfigWebApplicationContext 进行扫描
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();ctx.register(AppConfig.class);ctx.refresh();MyBean myBean = ctx.getBean(MyBean.class);
关联
对Controller进行增强,全局捕获spring MVC 抛出的异常
自动catch异常,并匹配响应的ExceptionHandler,重新封装。统一返回给前端 ```java @RestControllerAdvice(annotations = RestController.class) public class UniformReponseHandler
{ @ResponseStatus(HttpStatus.OK) public CallResultMsg sendSuccessResponse(){
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);
}
