Announcement Announcement Module
Collapse
No announcement yet.
@PostConstruct called twice Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @PostConstruct called twice

    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:
    Code:
            <context-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>/WEB-INF/contexts/appContext*.xml</param-value>
            </context-param>
    And I have
    Code:
            <listener>
                    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
            </listener>
    But no DispatcherServlet (I'm using JSF instead of spring MVC)


    In my appContext.xml, I have:
    Code:
            <context:component-scan base-package="[my package]" />
            <context:load-time-weaver />
            <aop:aspectj-autoproxy />
    in only one appContext*.xml file

    Because I want to use spring framework to inject servlet and JSF managed beans, so I defined
    Code:
            <Loader
                    loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"
                    useSystemClassLoaderAsParent="false" />
    In my context.xml.

    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:
    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	
    	DefaultAnnotationProcessor(DefaultAnnotationProcessor).postConstruct(Object) line: 96	
            ServletImpl(StandardWrapper).loadServlet() line: 1121
    And
    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
    Looks like one was called by load time weaver (init class), and one was called by annotation processor.

    Any idea?

    Thanks a lot

    Noodle

  • #2
    JSF / Spring integration can be achieved in quite a much simpler manner than that...it's just a matter of specifying a Spring EL resolver for managed beans in faces-config.xml:

    Code:
    <application>  <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>

    Comment


    • #3
      I tried to add this into my faces-config.xml and removed
      Code:
      <context:load-time-weaver>
      from appContext.xml, removed
      Code:
              <Loader
                      loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"
                      useSystemClassLoaderAsParent="false" />
      from context.xml

      No, that will not inject spring beans into my servlet. It may only inject into JSF managed bean?

      Originally posted by Enrico Pizzi View Post
      JSF / Spring integration can be achieved in quite a much simpler manner than that...it's just a matter of specifying a Spring EL resolver for managed beans in faces-config.xml:

      Code:
      <application>  <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
      </application>
      Last edited by Noodle05; Feb 24th, 2011, 02:08 PM.

      Comment

      Working...
      X