场景:
数据源使用了druid数据源,其中aop_pattern使用了com.hfi.*这种方式。
之前一切正常。引入了二方库之后,发现spring boot服务无法启动,报错:
[ ERROR] [2019-08-19 14:52:15] [main] org.springframework.boot.SpringApplication [839] - Application startup failed org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Jetty servlet container at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer.start(JettyEmbeddedServletContainer.java:143) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:297) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:145) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) at com.hfi.JpademoApplication.main(JpademoApplication.java:12) Caused by: java.lang.NullPointerException: null at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:176) at org.eclipse.jetty.servlet.FilterHolder.initialize(FilterHolder.java:137) at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:873) at org.springframework.boot.context.embedded.jetty.JettyEmbeddedWebAppContext$JettyEmbeddedServletHandler.deferredInitialize(JettyEmbeddedWebAppContext.java:46) at org.springframework.boot.context.embedded.jetty.JettyEmbeddedWebAppContext.deferredInitialize(JettyEmbeddedWebAppContext.java:36) at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer.handleDeferredInitialize(JettyEmbeddedServletContainer.java:186) at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer.start(JettyEmbeddedServletContainer.java:121) ... 10 common frames omitted |
代码跟踪进去,发现是二方库有一个Filter
public class ValidateCodeFilter extends OncePerRequestFilter implements InitializingBean {
这个Filter造成GenericFilterBean logger is null
而其他spring内部的Filter extends GenericFilterBean的,其是这样的
参考以下链接:
@RefreshScope GenericFilterBean will fail to initialize
问题就清楚了,二方库的Filter被druid cglib proxy了,导致其实例的logger为null
解决方案:
spring.datasource.druid.aop-patterns=com.hfi.controller.*,com.hfi.service.*,com.hfi.repository.*
以及
@Bean
public JdkRegexpMethodPointcut druidStatPointcut(){
JdkRegexpMethodPointcut druidStatPointcut = new JdkRegexpMethodPointcut();
String patterns = "com.hfi.controller.*,com.hfi.service.*,com.hfi.repository.*";
druidStatPointcut.setPatterns(patterns);
return druidStatPointcut;
}