Spring beans getting initialized twice - Spring Integration -
i'm trying integrate application spring integration , experiencing custom spring beans getting initialized twice, see init method on these beans getting called twice, once during server startup , second time when http request made through dispatcherservlet.
here web.xml configuration:
<servlet> <servlet-name>webapp</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/servlet-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>webapp</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> here servlet-config.xml (with namespaces removed)
<import resource="springbeans-config.xml"/> <context:component-scan base-package="com.test"/> <context:annotation-config/> <int:channel id="inboundchannel"/> <int:channel id="outboundchannel"/> <http:inbound-gateway request-channel="inboundchannel" reply-channel="outboundchannel" name="/*" supported-methods="get, post, put, delete" reply-timeout="120000"/> <int:chain input-channel="inboundchannel"> <int:service-activator ref="clearcontext"/> <int:service-activator ref="gatewayfilter"/> <int:service-activator ref="audit_logger"/> <int:service-activator ref="gatewaycontexthandler" method="process"/> </int:chain> the custom file springbeans-config.xml containing bean definitions imported shown above. e.g. bean definition below invoked twice, during server startup , while making http request gets invoked through dispatcherservlet.
<bean name="sample" class="com.test.sampleimpl" init-method="init"> <property name="xpathhelper" ref="xpathhelper"/> <property name="cachemanager" ref="cachemanager"/> </bean> wondering i'm missing here. appreciate pointers / on this. thanks.
===============================================================
update - solved
loanshark example within springintegration samples helped resolve issue.
here updated web.xml
<context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath*:meta-inf/spring/applicationcontext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>gateway</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/spring/servlet-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>gateway</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> updated servlet-config.xml (with namespaces removed). removed import bean definition file , component-scan , annotation-config in file.
<http:inbound-gateway request-channel="inboundchannel" reply-channel="outboundchannel" name="/*" supported-methods="get, post, put, delete" reply-timeout="120000"/> <int:chain input-channel="inboundchannel"> <int:service-activator ref="clearcontext"/> <int:service-activator ref="gatewayfilter"/> <int:service-activator ref="audit_logger"/> <int:service-activator ref="gatewaycontexthandler" method="process"/> </int:chain> renamed springbeans-config.xml applicationcontext.xml per sample, guess should not matter. note, there no imports in file too.
<context:component-scan base-package="com.test"/> <context:annotation-config/> <bean name="sample" class="com.test.sampleimpl" init-method="init"> <property name="xpathhelper" ref="xpathhelper"/> <property name="cachemanager" ref="cachemanager"/> </bean>
spring mvc apps have 2 contexts; servlet context , root context.
it's practice put "web" beans (@controllers, views, http inbound adatpers etc) in servlet context , "business" beans in root context.
instead of importing beans, should put them in root context using context loader listener.
beans in servlet context can references beans in root context, not vice-versa.
the root context loaded first; filename doesn't matter when using wildcards in contextconfiglocation need careful servlet context config not picked second time.
Comments
Post a Comment