I try to integrate my application with Spring integration and experience that my custom Spring beans receives initialization twice, basically I see that the init method on these beans is called twice, once during server startup and the second time when the HTTP request is executed through the DispatcherServlet.
Here is my 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 is my servlet-config.xml (with remote namespaces)
<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 springbeans-config.xml file containing all the bean definitions is imported as shown above. E.g. the bean definition below will be called twice during server startup and when making an HTTP request that is called through the DispatcherServlet.
<bean name="sample" class="com.test.SampleImpl" init-method="init"> <property name="xpathHelper" ref="XPathHelper"/> <property name="cacheManager" ref="cacheManager"/> </bean>
I wonder what I donβt see here. Any pointers / help on this subject will be appreciated. Thanks.
==================================================== ==============
UPDATE - SOLVED
Example
loanshark in SpringIntegration examples helped solve this problem.
Here is the 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 namespace removal). Remote import into the bean definition file and scan-component and annotation-config in this 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 to applicationContext.xml according to the sample, but I don't think it matters. Please note: there is no import in this file.
<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>