主题:springboot 停止时尝试重新初始化 druid 数据源

ktblack 2019年12月25日 44

springboot 停止时报错,貌似是停止时又尝试去创建dataSource

配置:springboot:2.2.2.RELEASE, druid-spring-boot-starter:1.1.21, beetl-framework-starter: 1.2.19.RELEASE

错误信息:

18:09:02.961 - INFO gistrar$SpringApplicationAdmin : Application shutdown requested.
18:09:02.962 -DEBUG letWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1fdf1c5, started on Wed Dec 25 18:08:11 CST 2019
18:09:02.965 -DEBUG .j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
18:09:02.965 -DEBUG .j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans
18:09:02.968 - INFO c.a.d.pool.DruidDataSource     : {dataSource-1} closing ...
18:09:02.974 - INFO c.a.d.pool.DruidDataSource     : {dataSource-1} closed
18:09:03.934 -DEBUG ionConfigWebApplicationContext : Closing WebApplicationContext for namespace 'cmsPub-servlet', started on Wed Dec 25 18:08:15 CST 2019, parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1fdf1c5
18:09:03.943 - INFO a.DruidDataSourceAutoConfigure : Init DruidDataSource
18:09:04.054 - INFO c.a.d.pool.DruidDataSource     : {dataSource-2} inited
18:09:04.054 - INFO .DruidDataSourceStatLoggerImpl : {"url":"jdbc:mysql://192.168.99.114:3306/mycms?characterEncoding=UTF-8&serverTimezone=UTC&useUnicode=true","dbType":"mysql","name":"DataSource-1501059253","activeCount":0,"poolingCount":3,"poolingPeak":3,"poolingPeakTime":"2019-12-25 18:09:04","connectCount":0,"closeCount":0,"physicalConnectCount":3}
18:09:04.068 - WARN ionConfigWebApplicationContext : Exception thrown from ApplicationListener handling ContextClosedEvent
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlManagerFactoryBean' defined in class path resource [com/ibeetl/starter/BeetlSqlSingleConfig.class]: Unexpected exception during bean creation; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.env.Environment' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:530)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207)
    at org.springframework.context.event.AbstractApplicationEventMulticaster.retrieveApplicationListeners(AbstractApplicationEventMulticaster.java:245)
    at org.springframework.context.event.AbstractApplicationEventMulticaster.getApplicationListeners(AbstractApplicationEventMulticaster.java:197)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:134)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:409)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1012)
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:978)
    at org.springframework.web.servlet.FrameworkServlet.destroy(FrameworkServlet.java:866)
    at org.apache.catalina.core.StandardWrapper.unload(StandardWrapper.java:1291)
    at org.apache.catalina.core.StandardWrapper.stopInternal(StandardWrapper.java:1592)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5384)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
    at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1400)
    at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1389)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
    at org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:976)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
    at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1400)
    at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1389)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
    at org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:976)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
    at org.apache.catalina.core.StandardService.stopInternal(StandardService.java:473)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
    at org.apache.catalina.core.StandardServer.stopInternal(StandardServer.java:992)
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
    at org.apache.catalina.startup.Tomcat.stop(Tomcat.java:469)
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.stopTomcat(TomcatWebServer.java:269)
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.stop(TomcatWebServer.java:324)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.stopAndReleaseWebServer(ServletWebServerApplicationContext.java:306)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onClose(ServletWebServerApplicationContext.java:172)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1035)
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:978)
    at org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar$SpringApplicationAdmin.shutdown(SpringApplicationAdminMXBeanRegistrar.java:160)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor44.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at com.sun.jmx.mbeanserver.ConvertingMethod.invokeWithOpenReturn(ConvertingMethod.java:193)
    at com.sun.jmx.mbeanserver.ConvertingMethod.invokeWithOpenReturn(ConvertingMethod.java:175)
    at com.sun.jmx.mbeanserver.MXBeanIntrospector.invokeM2(MXBeanIntrospector.java:117)
    at com.sun.jmx.mbeanserver.MXBeanIntrospector.invokeM2(MXBeanIntrospector.java:54)
    at com.sun.jmx.mbeanserver.MBeanIntrospector.invokeM(MBeanIntrospector.java:237)
    at com.sun.jmx.mbeanserver.PerInterface.invoke(PerInterface.java:138)
    at com.sun.jmx.mbeanserver.MBeanSupport.invoke(MBeanSupport.java:252)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
    at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
    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)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.env.Environment' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1695)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1253)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885)
    at org.springframework.beans.factory.support.ConstructorResolver.resolvePreparedArguments(ConstructorResolver.java:832)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:447)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    ... 83 common frames omitted

目前检查到是由于额外配置了一个DispatcherServlet:cmsPub-servlet,springboot停止时先销毁了dataSource-1,然后销毁 cmsPub-servlet 时不知道为什么又初始化了dataSource-2,就抛异常了。

cmsPub-servlet配置:

  private ServletRegistrationBean<DispatcherServlet> getApiDispatcher(String basePackage,
      String url, Integer startup, String name, Class<?> configClass) {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.scan(basePackage);
    context.register(configClass);
    DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
    ServletRegistrationBean<DispatcherServlet> bean = new ServletRegistrationBean<DispatcherServlet>(dispatcherServlet);
    bean.addUrlMappings(url);
    bean.setLoadOnStartup(startup);
    bean.setName(name);
    return bean;
  }
  
  @Bean
  public ServletRegistrationBean<DispatcherServlet> cmsPublicApi(DispatcherServlet dispatcherServlet1) {
    return getApiDispatcher(pubApiBasePackage, pubApiUrl, pubApiStartup, pubApiName, CmsApiPublicConfiguration.class);
  }
闲大赋 2019年12月25日

这个是不是你有别的应用又启用了,比如销毁的时候,逻辑里有使用数据库?

ktblack 2019年12月25日

这个没有,只有一个测试的controller,简单调一下查询。

而且发现只有在servlet自己new一个AnnotationConfigWebApplicationContext的时候就会有这种情况,使用注入的ApplicationContext的时候没有这样的情况。。。

  • 闲大赋 :这就不清楚了,只能调试一下看谁触发了(2019年12月27日)