这个是本人关于自定义注解第二篇文章,在很多时候,我们往往通过注解定义自己的拦截器来,做一些过滤的工作,这个时候采用切面和异常捕获处理往往可以达到非常好的预期,下面我示例一个某个IP访问次数限定的例子:
控制器代码:
package com.AnnotationTest;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.bind.annotation.RestController;
@Controller
public class MyController {
@RequestMapping("/index")
@ResponseBody
public String index(HttpServletRequest request) {
runner.Run(request);
return "this is my index";
}
@Autowired
private TestRunner runner;
}
异常处理器代码:
package com.AnnotationTest;
public class RequestExpection extends RuntimeException{
private static fina