java为什么拦截在方法之后?java怎么拦截mapper方法
其实java为什么拦截在方法之后的问题并不复杂,但是又很多的朋友都不太了解java怎么拦截mapper方法,因此呢,今天小编就来为大家分享java为什么拦截在方法之后的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!
怎么关闭广告拦截插件
怎样关闭工具栏的广告拦截功能?
1、浏览器提示时,会出现一个阻止了一个弹出窗口,然后点击选择总是允许此站点的弹出窗口直接点击就可以了。
2、打开IE,选择菜单栏的工具,然后选择internet选项,然后再选择隐私,在那一栏可以看到弹出窗口阻止程序。取消“启用弹出窗口阻止程序”前面的勾即可。
3、在IE浏览器的“工具”菜单上,指向“弹出窗口阻止程序”,然后单击“关闭弹出窗口阻止程序设置”即可。
怎样关闭百度浏览器拦截广告插件
到百度浏览器的应用中心里去,把拦截广告插件给卸载掉即可。
如何关闭“手机浏览器广告拦截插件”
1、首先,打开浏览器,点击右上角的菜单图标,弹出菜单,点击设置;
2、进入设置页面后,切换到扩展程序,然后点击获取更多扩展程序的连接;
3、进入网上应用店之后,在左边搜索栏里,搜索“广告屏蔽”,右边结果里,在扩展程序里寻找适合自己的一款应用即可;
4、找到应用后,点击后方的免费添加图标;
5、然后,弹出是否添加的确认提示框,点击添加,退出浏览器重新进入即可。
如何关闭广告屏蔽插件?
很高兴为您解答:
打开腾讯电脑管家一工具箱一广告过滤
祝楼主祝您龚作、生活愉快!!
手机浏览器广告拦截插件怎么关闭
1、浏览器主页面,点击页面右上角“设置”按钮,选择“管理加载项”选项。 2、在加载项页面中,点击工具栏选项,然后查看启用的插件,如果需要禁用的话,对其击右键,选择“禁用”。
电脑上的广告拦截功能怎么取消啊?
为什么要取消昂拦截不好么如果非要取消的话右键IE浏览器在属性里面设置
360怎么关闭广告拦截
在360设置里面,有个广告拦截设置,把勾打掉就行,望采纳
怎么关闭ie浏览器的去广告插件
方法/步骤 1、首先打开谷歌浏览器,然后点击设置按钮。谷歌浏览器的设置在浏览器的右上角上,点击后选择设置即可。 2、在谷歌浏览器的设置界面选择扩展程序。在扩展程序中我们可以添加一些插件也可以删除一些插件。 3、在扩展程序中点击下面的
Google Chrome怎么关闭广告拦截? 10分
打开,工具,扩展程序看看,然后勾上Adblock plus关闭这个插件!
怎么关闭flash广告拦截
你用的什么浏览器我不知道我的是360的在浏览器工具---网页设置中----可以禁止flas*** ActiveX脚本禁止脚本禁止java小程序我的是这样设置的进入网页无那些乱七八糟的广告网页垃圾游戏连接图片什么的非常好用网页超干净如是其他浏览器在工具浏览器选项中也有相应设置操作看看祝你好运
丁对您有所帮助无望采纳(手打)
java怎么拦截mapper方法
1、使用@Aspect注解
要去使用jdk的代理,否则代理不了mapper(即mybatis代理的mapper没有默认的构造器,cglib无法再给这个代理构造代理,会报如下错误org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy13]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy13)
<!--启动对@Aspectj的支持 true为cglib,false为jdk代理,为true的话,会导致拦截不了mybatis的mapper-->
<aop:aspectj-autoproxy proxy-target-class="false"/>
之后就是使用注解去配置拦截,进行修改记录的操作
@Aspect
@Component
public class DatalogAspect{
private static final Logger logger= LoggerFactory.getLogger(DatalogAspect.class);
@Resource
private ActionMapper actionMapper;
@Pointcut("execution(public* com.yami.infrastructure.repository.mapper.*.insert*(..))&&!execution(public* com.yami.infrastructure.repository.mapper.ActionMapper.insert*(..))")
public void insert(){
}
@Pointcut("execution(public* com.yami.infrastructure.repository.mapper.*.update*(..))")
public void update(){
}
@Pointcut("execution(public* com.yami.infrastructure.repository.mapper.*.delete*(..))")
public void delete(){
}
@Around("insert()|| update()|| delete()")
public Object addOperateLog(ProceedingJoinPoint pjp) throws Throwable{
...
}
}
2、使用MethodInterceptor
使用aopalliance的MethodInterceptor
(1)配置文件
去掉对@Aspect注解的支持(也可以不去掉,只要不是proxy-target-class= true就可以)。然后配置aop
<bean id="datalogInterceptor" class="com.yami.infrastructure.datalog.DatalogInterceptor"/>
<aop:config>
<aop:pointcut id="datalogInsertPointCut" expression="execution(* com.yami.infrastructure.repository.mapper..insert*(..))&&!execution(* com.yami.infrastructure.repository.mapper.ActionMapper.*(..))"/>
<aop:pointcut id="datalogUpdatePointCut" expression="execution(* com.yami.infrastructure.repository.mapper..update*(..))&&!execution(* com.yami.infrastructure.repository.mapper.ActionMapper.*(..))"/>
<aop:pointcut id="datalogDeletePointCut" expression="execution(* com.yami.infrastructure.repository.mapper..delete*(..))&&!execution(* com.yami.infrastructure.repository.mapper.ActionMapper.*(..))"/>
<aop:advisor advice-ref="datalogInterceptor" pointcut-ref="datalogInsertPointCut"/>
<aop:advisor advice-ref="datalogInterceptor" pointcut-ref="datalogUpdatePointCut"/>
<aop:advisor advice-ref="datalogInterceptor" pointcut-ref="datalogDeletePointCut"/>
</aop:config>
(2)实现MethodInterceptor
public class DatalogInterceptor implements MethodInterceptor{
private static final Logger logger= LoggerFactory.getLogger(DatalogInterceptor.class);
public DatalogInterceptor(){
}
@Resource
private ActionMapper actionMapper;
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable{
Method method= methodInvocation.getMethod();
String methodName= method.getName();
Class<?> cls= method.getDeclaringClass();
Object service= methodInvocation.getThis();
Object[] args= methodInvocation.getArguments();
Integer actionType=-1;
...
}
}
Java实现拦截HTTP请求的几种方式
在Java的服务端开发当中,拦截器是很常见的业务场景,这里对Java开发当中几种常见的拦截器的实现方式进行记录和分析。案例说明基于Spring Boot环境。
一:实现javax.servlet.Filter接口(使用过滤器方式拦截请求)
import org.springframework.stereotype.Component;import javax.servlet.*;import java.io.IOException;import java.util.Date;@Componentpublic class TimeInterceptor implements Filter{@Overridepublic void init(FilterConfig filterConfig) throws ServletException{System.out.println("time filter init");}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException{System.out.println("time filter start");long start= new Date().getTime();filterChain.doFilter(servletRequest, servletResponse);System.out.println("time filter耗时:"+(new Date().getTime()-start));System.out.println("time filter finish");}@Overridepublic void destroy(){System.out.println("time filter destroy");}}
如使用@Compent注解声明不需要加入其它配置即可使得拦截器生效,但是默认拦截/*,会拦截所有请求。
二:使用@Bean注入自定义拦截器,依然上面的代码,去掉@Compent注解,创建TimeWebConfig配置类:
import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.ArrayList;import java.util.List;@Configurationpublic class TimeWebConfig{@Beanpublic FilterRegistrationBean timeFilter(){FilterRegistrationBean registrationBean= new FilterRegistrationBean();TimeInterceptor interceptor= new TimeInterceptor();registrationBean.setFilter(interceptor);List<String> urls= new ArrayList<>();urls.add("/user/*");registrationBean.setUrlPatterns(urls);return registrationBean;}}
上面这两种拦截请求的实现是基于JavaEE提供的Filter接口实现的,缺点在于,该拦截器实际上是一个过滤器,执行代码的方法doFilter只提供了request,response等参数,当请求进入被过滤器拦截的时候,我们并不知道这个请求是由哪个控制器的哪个方法来执行的。
三:使用springMVC提供的拦截器,实现org.springframework.web.servlet.HandlerInterceptor接口:
创建自定义的拦截器:
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;@Componentpublic class MyInterceptor implements HandlerInterceptor{@Overridepublic boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception{System.out.println("preHandler");System.out.println(((HandlerMethod) handler).getBean().getClass().getName());System.out.println(((HandlerMethod) handler).getMethod().getName());httpServletRequest.setAttribute("start", new Date().getTime());return true;}@Overridepublic void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception{System.out.println("postHandler");Long start=(Long) httpServletRequest.getAttribute("start");System.out.println("time interceptor耗时:"+(new Date().getTime()-start));}@Overridepublic void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception{System.out.println("afterCompletion");Long start=(Long) httpServletRequest.getAttribute("start");System.out.println("time interceptor耗时:"+(new Date().getTime()-start));System.out.println("ex is:"+e);}}
创建配置类:
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configurationpublic class WebConfig extends WebMvcConfigurerAdapter{@Autowiredprivate MyInterceptor interceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry){registry.addInterceptor(interceptor).addPathPatterns("/user/*").excludePathPatterns("/blog/*");}}
此种方式的拦截器当中我们能够获取拦截的请求对应的类和方法的相关信息,缺点在于该handler对象无法获取具体执行方法的参数信息。
四:利用Spring的切面(AOP)实现拦截器:
引入jar包:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency>
创建切片类:
import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.stereotype.Component;import java.util.Date;@Aspect@Componentpublic class TimeAspect{@Around("execution(* com.qinker.controller.UserController.*(..))")public Object handlerControllerMethod(ProceedingJoinPoint point) throws Throwable{System.out.println("time aspect start");long start= new Date().getTime();Object[] args= point.getArgs();for(Object obj: args){System.out.println("arg is:"+obj);}Object obj= point.proceed();//具体方法的返回值System.out.println("aspect耗时:"+(new Date().getTime()-start));System.out.println("time aspect end");return obj;}}
aspectj基于AOP实现的拦截器功能十分强大,具体详解请参考spring官网网站的文档。
如果你还想了解更多这方面的信息,记得收藏关注本站。