Hi Forum,
I did some research on this @PostConstruct called twice, but didn't found my case.
The issue is: my @PostConstruct method been called twice in my web application.
In my web.xml, I defined:
And I haveCode:<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/contexts/appContext*.xml</param-value> </context-param>
But no DispatcherServlet (I'm using JSF instead of spring MVC)Code:<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
In my appContext.xml, I have:
in only one appContext*.xml fileCode:<context:component-scan base-package="[my package]" /> <context:load-time-weaver /> <aop:aspectj-autoproxy />
Because I want to use spring framework to inject servlet and JSF managed beans, so I defined
In my context.xml.Code:<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader" useSystemClassLoaderAsParent="false" />
I tried not use TomcatInstrumentableClassLoader, but servlet and JSF managed beans will not inject spring beans.
I tried not use ContextLoaderListener, but that also give Bean initial exception. My understand is if define DispatcherServlet, that will create another servlet context, but I'm not using it.
The call stack of two times are:
AndCode:Thread [main] (Suspended (breakpoint at line 56 in MyServlet)) MyServlet.springInit() line: 56 NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method] NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39 DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25 Method.invoke(Object, Object...) line: 597 DefaultAnnotationProcessor(DefaultAnnotationProcessor).postConstruct(Object) line: 96 ServletImpl(StandardWrapper).loadServlet() line: 1121
Looks like one was called by load time weaver (init class), and one was called by annotation processor.Code:Thread [main] (Suspended (breakpoint at line 56 in MyServlet)) MyServlet.springInit() line: 56 NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method] NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39 DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25 Method.invoke(Object, Object...) line: 597 InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(Object) line: 340 InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(Object, String) line: 293 CommonAnnotationBeanPostProcessor(InitDestroyAnnotationBeanPostProcessor).postProcessBeforeInitialization(Object, String) line: 130 DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).applyBeanPostProcessorsBeforeInitialization(Object, String) line: 394 DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).initializeBean(String, Object, RootBeanDefinition) line: 1413 DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).initializeBean(Object, String) line: 386 BeanConfigurerSupport.configureBean(Object) line: 141 AnnotationBeanConfigurerAspect.configureBean(Object) line: 59 AnnotationBeanConfigurerAspect(AbstractDependencyInjectionAspect).ajc$afterReturning$org_springframework_beans_factory_aspectj_AbstractDependencyInjectionAspect$2$1ea6722c(Object) line: 89 <unknown receiving type>(MyServlet).<init>() line: 37 NativeConstructorAccessorImpl.newInstance0(Constructor, Object[]) line: not available [native method] NativeConstructorAccessorImpl.newInstance(Object[]) line: 39 DelegatingConstructorAccessorImpl.newInstance(Object[]) line: 27 Constructor<T>.newInstance(Object...) line: 513 Class<T>.newInstance0() line: 355 Class<T>.newInstance() line: 308 ServletImpl(StandardWrapper).loadServlet() line: 1116
Any idea?
Thanks a lot
Noodle


Reply With Quote
