1.编写拦截器操作
package com.chen.springboot.demo.intercepter; import com.chen.springboot.demo.task.TimerTask; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class LoginIntercepter extends HandlerInterceptorAdapter { @Autowired com.chen.springboot.demo.task.TimerTask task; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle........"); return super.preHandle(request, response, handler); } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle......."); super.postHandle(request,response,handler,modelAndView); } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion......."); if (task == null) {//解决service为null无法注入问题 System.out.println("operatorLogService is null!!!"); BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()); task = factory.getBean(TimerTask.class); } task.startTask(); super.afterCompletion(request,response,handler,ex); } }
编写拦截器规则
package com.chen.springboot.demo; import com.chen.springboot.demo.intercepter.LoginIntercepter; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; //配置拦截器 @Configuration public class WebMvcConfigurerAdapte extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/**"); //registry.addInterceptor(new Demo1Interceptor()); super.addInterceptors(registry); } }
说明,在拦截器中,@Autowired会失效,
需要用以下的方式获取bean
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()); task = factory.getBean(TimerTask.class);
2.利用springboot打war包
a.需要在pom.xml中
<packaging>war</packaging>
<build> <finalName>demo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
b.修改启动类,继承SpringBootServletInitializer,并实现SpringApplicationBuilder configure(SpringApplicationBuilder application)方法:
package com.chen.springboot.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class TomcatApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(TomcatApplication.class); } public static void main(String[] args) { SpringApplication.run(TomcatApplication.class, args); } }
c.用maven打包
mvn clean package