Spring bean初始化两次 - Spring Integration

cod*_*mer 5 spring-mvc spring-integration

我正在尝试将我的应用程序与Spring Integration集成并体验我的自定义spring bean初始化两次,基本上我看到这些bean上的init方法被调用两次,一次是在服务器启动期间,第二次是在HTTP请求时通过DispatcherServlet制作.

这是我的web.xml配置:

  <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>
Run Code Online (Sandbox Code Playgroud)

这是我的servlet-config.xml(删除了名称空间)

<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>
Run Code Online (Sandbox Code Playgroud)

如上所示导入包含所有bean定义的自定义文件springbeans-config.xml.例如,下面的bean定义将在服务器启动期间调用两次,同时发出通过DispatcherServlet调用的HTTP请求.

    <bean name="sample" class="com.test.SampleImpl" init-method="init">
    <property name="xpathHelper" ref="XPathHelper"/>
    <property name="cacheManager" ref="cacheManager"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

想知道我在这里失踪了什么.非常感谢任何指针/帮助.谢谢.

================================================== =============

更新 - 已解决

SUMMIntegration示例中的loanhark示例有助于解决此问题.

这是更新的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>
Run Code Online (Sandbox Code Playgroud)

更新了servlet-config.xml(删除了名称空间).删除了此文件中对bean定义文件以及component-scan和annotation-config的导入.

<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>
Run Code Online (Sandbox Code Playgroud)

根据示例将springbeans-config.xml重命名为applicationContext.xml,但我想这应该不重要.请注意,此文件中也没有导入.

<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>
Run Code Online (Sandbox Code Playgroud)

Gar*_*ell 5

Spring MVC Apps通常有2个上下文; servlet上下文和根上下文.

将"web"bean(@Controllers,views,Http inbound adatpers等)放入servlet上下文和根上下文中的所有"business"bean通常是一种好习惯.

您应该使用上下文加载器侦听器将它们放在根上下文中,而不是导入bean.

servlet上下文中的Bean可以在根上下文中获取对bean的引用,但反之亦然.

首先加载根上下文; 文件名无关紧要,但在使用通配符时,contextConfigLocation需要注意不要再次拾取servlet上下文配置.

  • 好吧,它们并不是真正独立的 - 根上下文是servlet上下文的父级(允许您从服务激活器引用bean),但是正确; 没有进口.这是个人喜好; 在servlet上下文中包含所有SI组件可以生成一个很好的图形(如果使用SI集成图形查看器).有些人只在servlet上下文中放入http入站适配器,在根上下文中放置了其余的流(从第一个通道开始). (2认同)