java拦截器和切面_Filter 过滤器,Interception 拦截器,Aspect 切面 三者不同点

package com.lzh.web.filter;

import javax.servlet.*;

import java.io.IOException;

import java.util.Date;

/**

* @author li

* 2019/10/1 16:09

* version 1.0

*/

// 可以通过注解注入 或者通过MVC配置加载

//@Component

public class TimeFilter implements Filter {

@Override

public void init(FilterConfig filterConfig) throws ServletException {

System.out.println("过滤器 filter init....");

}

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

System.out.println("过滤器 进入..doFilter..");

long start = System.currentTimeMillis();

chain.doFilter(request, response);

System.out.println("过滤器 执行用时: " + (System.currentTimeMillis() - start));

System.out.println("过滤器 拦截器结束...");

}

@Override

public void destroy() {

System.out.println("过滤器 filter destroy....");

}

}

通过配置加载 Filter

package com.lzh.web.config;

import com.lzh.web.filter.TimeFilter;

import com.lzh.web.interceptor.TimeInterceptior;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.web.servlet.FilterRegistrationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;

import java.util.List;

/**

* @author li

* 2019/10/1 16:45

* version 1.0

*/

@Configuration

public class WebConfig extends WebMvcConfigurerAdapter {

@Autowired

private TimeInterceptior timeInterceptior;

@Override

public void addInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(timeInterceptior);

}

@Bean

public FilterRegistrationBean timeFilter(){

FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();

filterRegistrationBean.setFilter(new TimeFilter());

List url = new ArrayList<>();

url.add("/*");

filterRegistrationBean.setUrlPatterns(url);

return filterRegistrationBean;

}

}

package com.lzh.web.interceptor;

import org.omg.PortableInterceptor.Interceptor;

import org.springframework.stereotype.Component;

import org.springframework.web.method.HandlerMethod;

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.util.Date;

/**

* @author li

* 2019/10/1 19:57

* version 1.0

*/

@Component

public class TimeInterceptior implements HandlerInterceptor {

//前置

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

System.out.println("拦截器 preHandle...");

System.out.println("拦截器 类名字: "+((HandlerMethod) handler).getBean().getClass().getName());

System.out.println("拦截器 方法名字: "+((HandlerMethod)handler).getMethod().getName());

request.setAttribute("start", System.currentTimeMillis());

return true;

}

//没有遇到异常 就进入

@Override

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

System.out.println("拦截器 postHandle...");

Long start = (Long) request.getAttribute("start");

System.out.println("拦截器 postHandle 耗时: " + (System.currentTimeMillis() - start));

}

//结束

@Override

public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

System.out.println("拦截器 afterCompletion...");

Long start = (Long) request.getAttribute("start");

System.out.println("拦截器 afterCompletion 耗时: " + (System.currentTimeMillis() - start));

System.out.println("拦截器 异常类型: " + ex);

}

}

package com.lzh.web.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.springframework.stereotype.Component;

/**

* @author li

* 2019/10/2 11:23

* version 1.0

*/

@Aspect

@Component

public class TimeAspect {

@Around("execution(* com.lzh.web.controller.UserController.*(..))")

public Object handlerControllerMethod(ProceedingJoinPoint pjp) throws Throwable {

System.out.println("AOP 开始...");

Long start = System.currentTimeMillis();

Object[] args = pjp.getArgs();

for (Object arg : args) {

System.out.println("参数: " + arg);

}

Object object = pjp.proceed();

System.out.println("AOP结束 end: "+(System.currentTimeMillis() - start));

return object ;

}

}

过滤器 进入..doFilter..

拦截器 preHandle...

拦截器 类名字: com.lzh.web.controller.UserController$$EnhancerBySpringCGLIB$$963e03f

拦截器 方法名字: getinfo

AOP 开始...

参数: 1

进入..getinfo...

AOP结束 end: 5

拦截器 postHandle...

拦截器 postHandle 耗时: 95

拦截器 afterCompletion...

拦截器 afterCompletion 耗时: 95

拦截器 异常类型: null

过滤器 执行用时: 115

过滤器 拦截器结束...

过滤器可以拿到原始http的请求和相应信息,无法获得处理请求的方法的信息

拦截器可以获得过滤器获得的内容,但是无法获得调用http请求 具体的参数信息

切面 只可以获得调用的 具体参数

V4mV4b0cpg6ZFfTGEkhHCIAABCEAAAhCAAAQgMCTQe+q8JDKVIYZzM+aQHccQgAAEIAABCEAAAhMEdMslHwhAAAIQgAAEIAABCGxOgLUNNkeKQQhAAAIQgAAEIAABEUBo4gcQgAAEIAABCEAAAlchgNC8ClaMQgACEIAABCAAAQj8P4meNDcOP8oDAAAAAElFTkSuQmCC

注意:本文归作者所有,未经作者允许,不得转载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值