package com.newtv.search.serviceapi.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.newtv.search.serviceapi.common.Const;
import com.newtv.search.serviceapi.service.impl.RedisInterceptor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@Configuration
public class CorsConfig extends WebMvcConfigurationSupport {
@Value(value = "${spring.autoconfigure.exclude}")
private String redisOn;
/**
* 在springboot2.0.0之后继承WebMvcConfigurationSupport类,重写addInterceptors方法
*
* @param registry
*/
@Override
protected void addInterceptors(InterceptorRegistry registry) {
/**
* 拦截器按照顺序执行,如果不同拦截器拦截存在相同的URL,前面的拦截器会执行,后面的拦截器将不执行
*/
if(StringUtils.isBlank(redisOn)){
RedisInterceptor redisInterceptor = new RedisInterceptor();
registry.addInterceptor(redisInterceptor)
//配置需要拦截的路径
.addPathPatterns("/**")
//配置不需要拦截的路径
.excludePathPatterns("/hn/saveCategory/**","/library/contents/**",
"/hn/library/contents/**","/es/**");
super.addInterceptors(registry);
}
}
}
在拦截其中获取实例
启动任务使用SpringContextUtil
/**
* 拦截器中实例化redisutil
*
* @param args
*/
public static void main(String[] args) {
SpringContextUtil springContextUtil = new SpringContextUtil();
ApplicationContext applicationContext = SpringApplication.run(PtServiceApiServerApplication.class, args);
springContextUtil.setApplicationContext(applicationContext);
//SpringApplication.run(PtServiceApiServerApplication.class, args);
}
拦截器中获取指定的实例
RedisUtil redisUtil = (RedisUtil) SpringContextUtil.getBean(RedisUtil.class);