标签:spring Interception springframework import org config servlet annotation
拦截器
-
拦截器(Interceptor)是一种动态拦截方法调用的机制,在SpringMVC中动态拦截控制器方法的执行
-
作用:在指定的方法调用前后执行预先设定的代码,阻止原始方法的执行
拦截器&过滤器
-
归属不同:Filter属于Servlet技术,Interception属于SpringMVC技术
-
拦截内容不同:Filter对所有访问进行增强,Interception仅针对SpringMVC的访问进行增强。
拦截器执行流程
---perHandle --->return----->controller----->postHandle---->afterCompletion----->
---->false---------------------------跳过这一环了-------------------------->
代码
-
Interception
package com.yang.controller.Interception;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class ProjectInterception implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle...");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle...");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion...");
}
}
-
SpringMvcSupport
package com.yang.config;
import com.yang.controller.Interception.ProjectInterception;
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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Autowired
private ProjectInterception projectInterception;
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(projectInterception).addPathPatterns("/books");
}
}
package com.yang.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan({"com.yang.controller","com.yang.config"})
@EnableWebMvc//开启接受json
public class SpringMvcConfig {
}
配置拦截器链
-
拦截器链的运行顺序
-
true
-
perHandle:与配置顺序相同,必定运行
-
postHandle:与配置顺序相反,可能不运行
-
afterCompletion:与配置顺序相反,可能不运行
-
-
false
-
perHandle:终止了
-
postHandle; 终止
-
afterCompletion:到哪一个perHandle终止,则执行它前面的afterCompletion。
-
-
标签:spring,Interception,springframework,import,org,config,servlet,annotation 来源: https://www.cnblogs.com/yzlworld/p/16500274.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。