首页 新闻 会员 周边 捐助

请问@PostConstruct注解执行两次怎么解决

1
[已解决问题] 解决于 2017-11-23 10:11

我在项目中初始化一个方法 在启动时运行

@Service
public class InitQuery {

    @Resource
    public CacheService cacheService;
    @Reference
    private IAkService akService;
    @PostConstruct
    public void queryAK() {
    }    
}

但是在启动时此方法缺执行了两次,后将栈方法打印

//第一次运行
java.lang.Thread.getStackTrace(Thread.java:1559)
com.mortgage.controller.init.InitQuery.queryAK(InitQuery.java:24)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1581)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4633)
org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5095)
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1425)
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1415)
java.util.concurrent.FutureTask.run(FutureTask.java:266)
org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:941)
org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1425)
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1415)
java.util.concurrent.FutureTask.run(FutureTask.java:266)
org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:941)
org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258)
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
org.apache.catalina.startup.Catalina.start(Catalina.java:657)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:355)
org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:495)
com.mortgage.controller.init.InitQuery@4f52afb9
启动
//第二次运行
java.lang.Thread.getStackTrace(Thread.java:1559)
com.mortgage.controller.init.InitQuery.queryAK(InitQuery.java:24)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1581)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1174)
org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1090)
org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:980)
org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4819)
org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5129)
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1425)
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1415)
java.util.concurrent.FutureTask.run(FutureTask.java:266)
org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:941)
org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1425)
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1415)
java.util.concurrent.FutureTask.run(FutureTask.java:266)
org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:941)
org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258)
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
org.apache.catalina.startup.Catalina.start(Catalina.java:657)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:355)
org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:495)
com.mortgage.controller.init.InitQuery@3bb7c155
启动

经查询据说可能是因为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>
    
    <!-- Spring MVC servlet -->
    <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>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
<dubbo:annotation package="com.mortgage.controller." />
<!-- 配置扫描的包 -->
<context:component-scan base-package="com.mortgage." />

但其解决方法为更改源码 后续影响未知。

另有文章说改为service注解即可解决 但更改后发现并没有起到作用 因此提问各位大神希望能解决此问题

参考文章:

http://www.cnblogs.com/mypm/p/3164737.html

http://blog.csdn.net/lxb_champagne/article/details/17614099

热血中年人的主页 热血中年人 | 菜鸟二级 | 园豆:204
提问于:2017-11-22 15:59
< >
分享
最佳答案
0

经过排查发现最终原因还是自己将配置文件扫描了两次导致

在ContextLoaderListener的配置下

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-servlet.xml,classpath:spring-shiro.xml,classpath:spring-redis.xml</param-value>
    </context-param>

在DispactherServlet下是

<init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-servlet.xml</param-value>
        </init-param>

使得配置文件被扫描了两次 因此我将ContextLoaderListener注掉后 将其配置放入DispactherServlet下

<init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-servlet.xml,classpath:spring-shiro.xml,classpath:spring-redis.xml</param-value>
        </init-param>

使得问题解决

热血中年人 | 菜鸟二级 |园豆:204 | 2017-11-23 10:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册