Java Spring:在声明多个Dispatcher的情况下访问正确的WebApplicationContext

Java Spring:在声明多个Dispatcher的情况下访问正确的WebApplicationContext,java,apache-flex,spring,Java,Apache Flex,Spring,我在应用程序中声明了两个Spring上下文—一个用于Spring MVC请求,另一个用于Flex/BlazeDS messagebroker请求,映射到不同的url模式: <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/app/*</url-pattern> </servlet-mapping> <servl

我在应用程序中声明了两个Spring上下文—一个用于Spring MVC请求,另一个用于Flex/BlazeDS messagebroker请求,映射到不同的url模式:

<servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>flex</servlet-name>
    <url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
这种方法在我使用单个上下文运行时有效。但是,它还需要支持运行多个上下文的情况

设置断点时,我看到
springContext
的值是根上下文,只有一个configLocation-
/WEB-INF/applicationContext.xml

我假设这就是问题所在,因为上述代码所需的
iseralizer
是在
flexservlet.xml
中声明的

如何修改上述代码以支持这两种方案?(单个上下文和多个上下文)

编辑: 上面显示的代码位于内部,看起来像是一个自定义bean工厂。似乎
ApplicationContextAware
接口在生成的类上不受尊重。例如:

<bean id="dpHibernateRemotingAdapterComponentFactory"
    class="org.springframework.flex.core.ManageableComponentFactoryBean">
    <constructor-arg
        value="org.dphibernate.adapters.RemotingAdapter" />
    <property name="properties">
        <value>
            {"dpHibernate" :
                {
                    "serializerFactory" : "org.dphibernate.serialization.SpringContextSerializerFactory"
                }
            }
        </value>
    </property>
</bean>

{“dpHibernate”:
{
“serializerFactory”:“org.dphibernate.serialization.SpringContextSerializerFactory”
}
}

上面引用的代码位于
org.dphibernate.serialization.SpringContextSerializerFactory
中。使此
SpringContextSerializerFactory
实现
ApplicationContextAware
没有影响。

将自定义组件声明为Spring上下文感知:

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public MyCustomBean  implements ApplicationContextAware {

  private ApplicationContext springContext;

  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    springContext = applicationContext;
  }

  public ISerializer getSerializer(Object source,boolean useAggressiveSerialization)
{
    String serializerBeanName = springContext.getBeanNamesForType(ISerializer.class);
  }
}

在bean初始化时,Spring将访问bean的
setApplicationContext
方法,将创建它的上下文作为参数传递。如果
flex
是一个
DispatcherServlet
,并且由于某些原因您不能遵循Tomás Narros的建议,您可以使用
RequestContextils.getWebApplicationContext(request)
获得与当前
DispatcherServlet
关联的上下文


还有一个方便的方法
requestContextIls.getWebApplicationContext(request,ctx)
,如果
DispatcherServlet
的一个不可用,它将返回根上下文。

Hrmmmm…..我的Spring/Flex应用程序中几乎有这样的声明,使用Spring/Flex集成,并且只有一个应用程序上下文。这可能是问题所在吗?您在Flex上下文文件中声明了不在MVC上下文文件中的bean,并且它们没有真正被加载?

好建议-谢谢。不幸的是,它似乎不起作用。我已经用与此问题相关的配置更新了原始问题。您好。这里关键的一点是有两个调度员——这是有充分理由的。一个路由Spring MVC请求,另一个路由Flex messagebroker请求。两者是分开的,因此路由不会相互干扰。使用Spring BlazeDS,您也可以使用Spring MVC DispatcherServlet处理AMF/Flex请求,因此我不确定您为什么需要两个不同的上下文。+1。在某些环境中非常有用@马蒂:如果这确实解决了你的问题,请接受axtavt的回答。
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public MyCustomBean  implements ApplicationContextAware {

  private ApplicationContext springContext;

  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    springContext = applicationContext;
  }

  public ISerializer getSerializer(Object source,boolean useAggressiveSerialization)
{
    String serializerBeanName = springContext.getBeanNamesForType(ISerializer.class);
  }
}