spring boot jar包可以正常启动,打包成war包放在tomcat就报错
问题报错:
08-Apr-2020 16:52:36.873 信息 [mysql-cj-abandoned-connection-cleanup] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1384)
at org.apache.catalina.loader.WebappClassLoaderBase.getResource(WebappClassLoaderBase.java:1034)
at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.checkThreadContextClassLoader(AbandonedConnectionCleanupThread.java:117)
at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:84)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
后来发现问题的根本原因根本不在这,而是下面的问题报错
代码
@Slf4j
@Component
@WebFilter(filterName = "loginFilter", urlPatterns = "/*")
public class LoginFilter implements Filter {
@Autowired
private NoAuthConfig noAuthConfig;
private String[] noAuthUrls;
@Autowired
private RedisCache redisCache;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//初始化,无需鉴权链接
initNoAuthUrl(noAuthConfig.getNotAuthUrls()); // 这行代码报错
}
}
发现在使用@Webfilter注解Filter的情况下,不上外部tomcat时是没有问题的。但是在tomcat下运行时,filter中注入的bean就都是null
处理方法
@Configuration
@Slf4j
public class WebConfig {
@Bean
public Filter loginFilter(){
return new LoginFilter();
}
@Bean
public FilterRegistrationBean webTokenFilterRegistration(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(loginFilter());
registrationBean.addUrlPatterns("/*");
registrationBean.setName("loginFilter");
// 注意这个order顺序
registrationBean.setOrder(1800);
log.info("***************************loginFilter初始化***************************");
return registrationBean;
}
}
去掉 @Component 、@WebFilter注解
@Slf4j
public class LoginFilter implements Filter {
@Autowired
private NoAuthConfig noAuthConfig;
private String[] noAuthUrls;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//初始化,无需鉴权链接
initNoAuthUrl(noAuthConfig.getNotAuthUrls());
}
}