Spring boot注解总结
配置信息获取
从application.properties或application.xml中获取配置信息:
@Value("${前缀.键名}") //注解在属性,不能注解在静态属性上
@ConfigurationProperties(prefix="前缀") //注解在类上,配合@Component使用全局异常处理器
import edu.recipePost.pojo.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice // 定义全局变量处理器的注解
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class) // 指定捕捉的错误类型
public Result ex(Exception e) {
e.printStackTrace();
return Result.error("非法操作");
}
}