Spring beans initialized twice - Spring Integration

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> 
+4
source share
1 answer

Spring MVC applications usually have 2 contexts; servlet context and root context.

As a general rule, it is recommended that you use your "website" beans (@Controllers, views, Http inbound adatpers, etc.) in the servlet context and all the "business" beans in the root context.

Instead of importing beans, you should place them in the root context using the context loader listener.

Beans in the servlet context can get references to beans in the root context, but not vice versa.

The root context is loaded first; the file name does not matter, but when using wildcards in contextConfigLocation you need to be careful that the servlet context config is not received a second time.

+5
source

Source: https://habr.com/ru/post/1497358/


All Articles