因需求在项目中增加了项目启动时执行的方法,但是由于web.xml中的配置必须有ContextLoaderListener和DispatcherServlet 所以项目启动时在创建对象时和对象注入时会执行两次该方法
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
调出栈方法显示如下:
Thread.st=====================java.lang.Thread.getStackTrace(Thread.java:1559)
Thread.st=====================com.mortgage.controller.init.InitQuery.queryAK(InitQuery.java:25)
Thread.st=====================sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Thread.st=====================sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Thread.st=====================sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Thread.st=====================java.lang.reflect.Method.invoke(Method.java:498)
Thread.st=====================org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
Thread.st=====================org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
Thread.st=====================org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
Thread.st=====================org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
Thread.st=====================org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1581)
Thread.st=====================org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554)
Thread.st=====================org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
Thread.st=====================org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
Thread.st=====================org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
Thread.st=====================org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
Thread.st=====================org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
Thread.st=====================org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
Thread.st=====================org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
Thread.st=====================org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
Thread.st=====================org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
Thread.st=====================org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
Thread.st=====================org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
Thread.st=====================org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4633)
Thread.st=====================org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5095)
Thread.st=====================org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
Thread.st=====================org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1425)
Thread.st=====================org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1415)
Thread.st=====================java.util.concurrent.FutureTask.run(FutureTask.java:266)
Thread.st=====================org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
Thread.st=====================java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
Thread.st=====================org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:941)
Thread.st=====================org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
Thread.st=====================org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
Thread.st=====================org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1425)
Thread.st=====================org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1415)
Thread.st=====================java.util.concurrent.FutureTask.run(FutureTask.java:266)
Thread.st=====================org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
Thread.st=====================java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
Thread.st=====================org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:941)
Thread.st=====================org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258)
Thread.st=====================org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
Thread.st=====================org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
Thread.st=====================org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
Thread.st=====================org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
Thread.st=====================org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
Thread.st=====================org.apache.catalina.startup.Catalina.start(Catalina.java:657)
Thread.st=====================sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Thread.st=====================sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Thread.st=====================sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Thread.st=====================java.lang.reflect.Method.invoke(Method.java:498)
Thread.st=====================org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:355)
Thread.st=====================org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:495)
启动
十一月 20, 2017 2:35:16 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'SpringMVC'
Thread.st=====================java.lang.Thread.getStackTrace(Thread.java:1559)
Thread.st=====================com.mortgage.controller.init.InitQuery.queryAK(InitQuery.java:25)
Thread.st=====================sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Thread.st=====================sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Thread.st=====================sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Thread.st=====================java.lang.reflect.Method.invoke(Method.java:498)
Thread.st=====================org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
Thread.st=====================org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
Thread.st=====================org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
Thread.st=====================org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
Thread.st=====================org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1581)
Thread.st=====================org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554)
Thread.st=====================org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
Thread.st=====================org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
Thread.st=====================org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
Thread.st=====================org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
Thread.st=====================org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
Thread.st=====================org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
Thread.st=====================org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
Thread.st=====================org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
Thread.st=====================org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
Thread.st=====================org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
Thread.st=====================org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
Thread.st=====================org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
Thread.st=====================org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
Thread.st=====================org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
Thread.st=====================javax.servlet.GenericServlet.init(GenericServlet.java:158)
Thread.st=====================org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1174)
Thread.st=====================org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1090)
Thread.st=====================org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:980)
Thread.st=====================org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4819)
Thread.st=====================org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5129)
Thread.st=====================org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
Thread.st=====================org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1425)
Thread.st=====================org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1415)
Thread.st=====================java.util.concurrent.FutureTask.run(FutureTask.java:266)
Thread.st=====================org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
Thread.st=====================java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
Thread.st=====================org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:941)
Thread.st=====================org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
Thread.st=====================org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
Thread.st=====================org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1425)
Thread.st=====================org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1415)
Thread.st=====================java.util.concurrent.FutureTask.run(FutureTask.java:266)
Thread.st=====================org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
Thread.st=====================java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
Thread.st=====================org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:941)
Thread.st=====================org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258)
Thread.st=====================org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
Thread.st=====================org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
Thread.st=====================org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
Thread.st=====================org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
Thread.st=====================org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
Thread.st=====================org.apache.catalina.startup.Catalina.start(Catalina.java:657)
Thread.st=====================sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Thread.st=====================sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Thread.st=====================sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Thread.st=====================java.lang.reflect.Method.invoke(Method.java:498)
Thread.st=====================org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:355)
Thread.st=====================org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:495)
启动
从上代码中可看到执行两次的具体情况 经查阅 有人说将@Component注解改为@Service注解即可 但尝试后发想并没有起作用 而且由于本项目不能注掉ContextLoaderListener 故想知道如果解决此问题
后经过排查发现原因还是扫描了两遍`<context-param>