springboot配置拦截器和打war包

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

转载于:https://my.oschina.net/u/2528821/blog/1539354

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值