我在我的應用程序中聲明兩個彈簧環境 - 一個用於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.xml
和flex-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這是問題 - 與上面的代碼需要聲明的ISerializer
在flex-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
沒有影響。
一個很好的實用工具知道,+1 – Bozho 2010-10-07 13:29:24
這個伎倆!非常感謝 – 2010-10-07 13:56:34
+1。在一些環境中非常有用。 @馬蒂:如果這確實解決了你的問題,接受axtavt答案。 – 2010-10-08 06:40:18