在SpringBoot的项目中经常会遇到自定义注解的情况,同时需要根据注解来对使用它的类、方法或属性进行操作。下面主要是使用拦截器的方式进行注解的拦截判断,这里主要是使用pom的方式进行开发。
1.因为是使用的拦截器,所以必须导入spring-boot-starter-web的引用:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
2.自定义一个注解:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author liaoyubo
* @version 1.0 2017/9/14
* @description 自定义一个注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoggerAnnotation {
}
3.自定义一个拦截器,用于注解的拦截:
import com.annotation.log.LoggerAnnotation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
/**
* @author liaoyubo
* @version 1.0 2017/9/14
* @description 通过拦截器的方式实现自定义的日志记录
*/
public class LoggerInterceptor extends HandlerInterceptorAdapter{
private Logger logger = LoggerFactory.getLogger(LoggerInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod)handler;
Method method = handlerMethod.getMethod();
//获取当前方法上的指定注解
LoggerAnnotation loggerAnnotation = method.getAnnotation(LoggerAnnotation.class);
//判断当前注解是否存在
if(loggerAnnotation != null){
long startTime = System.currentTimeMillis();
request.setAttribute("startTime",startTime);
logger.info("进入" + method.getName() + "方法的时间是:" + startTime);
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod)handler;
Method method = handlerMethod.getMethod();
//获取当前方法上的指定注解
LoggerAnnotation loggerAnnotation = method.getAnnotation(LoggerAnnotation.class);
//判断当前注解是否存在
if(loggerAnnotation != null){
long endTime = System.currentTimeMillis();
long startTime = (Long) request.getAttribute("startTime");
long periodTime = endTime - startTime;
logger.info("离开" + method.getName() + "方法的时间是:" + endTime);
logger.info("在" + method.getName() + "方法的时长是:" + periodTime);
}
}
}
4.将拦截器类注入到适配器中,否则不能正常拦截访问。
import com.annotation.interceptor.LoggerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @author liaoyubo
* @version 1.0 2017/9/14
* @description 注册自定义的拦截器
*/
@Configuration
@EnableWebMvc
@ComponentScan
public class InterceptorRegister extends WebMvcConfigurerAdapter {
@Bean
public LoggerInterceptor loggerInterceptor(){
return new LoggerInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(loggerInterceptor());
}
}
5.定义一个restful类用于测试:
import com.annotation.log.LoggerAnnotation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author liaoyubo
* @version 1.0 2017/9/14
* @description
*/
@RestController
public class LoginController {
@RequestMapping("/login")
@LoggerAnnotation
public void login(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
启动项目,通过访问连接地址http://localhost:8080/annotationTest就可以得到以下
的输出结果:
2017-09-15 13:52:27.015 INFO 407336 --- [nio-8080-exec-1] c.a.interceptor.LoggerInterceptor : 进入login方法的时间是:1505454747015 2017-09-15 13:52:32.202 INFO 407336 --- [nio-8080-exec-1] c.a.interceptor.LoggerInterceptor : 离开login方法的时间是:1505454752202 2017-09-15 13:52:32.202 INFO 407336 --- [nio-8080-exec-1] c.a.interceptor.LoggerInterceptor : 在login方法的时长是:5187