2010-10-07 30 views
3

我在我的應用程序中聲明兩個彈簧環境 - 一個用於Spring的MVC請求,另一個面向Flex/BlazeDS的messagebroker請求映射到不同的URL的方式:春:訪問多個調度員正確的WebApplicationContext宣佈

<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> 

聲明瞭一個公共上下文配置(/WEB-INF/applicationContext.xml),然後這兩個上下文中的每一個都分別在spring-mvc-servlet.xmlflex-servlet.xml中聲明瞭它們自己的配置。

Inside flex-servlet.xml我有flex聲明特定的flex上下文。但是,當電話進入http://localhost/messagebroker/*時,我收到錯誤,說明這些bean不可用。

有問題的代碼是一個自定義Spring組件裏面,所以直接以訪問聲明豆引用WebApplicationContext:當我用一個單一的環境中運行

public ISerializer getSerializer(Object source,boolean useAggressiveSerialization) 
{ 
    ServletContext ctx = FlexContext.getServletContext(); 
    WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(ctx); 
    String serializerBeanName = springContext.getBeanNamesForType(ISerializer.class); 
} 

這種方法的工作原理。但是它也需要支持有多個上下文運行的地方。

設置斷點,我看到的springContext值是根上下文,用一個單一的configLocation - /WEB-INF/applicationContext.xml

我asssuming這是問題 - 與上面的代碼需要聲明的ISerializerflex-servlet.xml

如何修改上述代碼以支持兩種情況? (單個上下文和多個上下文)?

編輯: 上面顯示的代碼位於一個ManageableComponentFactoryBean,這似乎作爲自定義bean工廠操作的內部。看起來接口不符合生成的類。例如:

<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> 

上面引用的代碼位於org.dphibernate.serialization.SpringContextSerializerFactory之內。使這SpringContextSerializerFactory實施ApplicationContextAware沒有影響。

回答

3

如果flexDispatcherServlet,出於某種原因,你不能跟着托馬斯Narros的建議,您可以獲取使用RequestContextUtils.getWebApplicationContext(request)當前DispatcherServlet相關的上下文。

還有一種方便的方法RequestContextUtils.getWebApplicationContext(request, ctx),如果DispatcherServlet的一個不可用,它將返回根上下文。

+0

一個很好的實用工具知道,+1 – Bozho 2010-10-07 13:29:24

+0

這個伎倆!非常感謝 – 2010-10-07 13:56:34

+0

+1。在一些環境中非常有用。 @馬蒂:如果這確實解決了你的問題,接受axtavt答案。 – 2010-10-08 06:40:18

1

聲明您的自定義componente爲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); 
    } 
} 

在豆初始化,Spring將訪問bean的方法setApplicationContext,作爲參數傳遞它正在創造至極的環境。在那裏,你可以隨時隨地使用它。

+0

好點,+1。 – Bozho 2010-10-07 13:15:42

+0

好的建議 - 謝謝。毫無疑問,它似乎並不奏效。我已經更新了與此問題相關的相關配置的原始問題。 – 2010-10-07 13:43:43

0

Hrmmmm .....我在Spring/Flex應用程序中使用了Spring/Flex集成,並且只有一個應用程序上下文。這可能是問題嗎?您在Flex上下文文件中聲明瞭不在MVC上下文文件中的bean,並且它們並未真正加載?

+0

嗨。這裏關鍵的一點是,有兩個調度員 - 出於很好的理由。一個路由Spring-MVC請求,另一個Flex消息經紀人請求。兩者是分開的,所以路由不會相互干擾。 – 2010-10-07 13:57:22

+0

使用Spring BlazeDS,你可以使用Spring MVC DispatcherServlet來處理AMF/Flex請求,所以我不確定爲什麼你需要兩個不同的上下文。 – 2010-10-08 16:30:46