從形勢來看,如果應用不使用
Spring
就感覺有點落伍——說法有點別扭:好像有點過。誠然,
Spring
給我們帶來了太多的好處,以至于幾乎大部分的產品都以聲稱能夠整合
Spring
為榮,
Mule
也不能免俗:)
從官方來看,
mule
與
spring
的結合有三種做法:
1
、
Using Spring as a Component Factory
How to configure the Spring Container with Mule so that Managed components and other Mule objects can be loaded from Spring.
2
、
Configuring the Mule Server From a Spring Context
A Mule server is just a bunch of beans! How to load a Mule instance from the Spring Container.
3
、
Configuring a Spring context using Mule Xml
There are lots of reasons why you might want to use Mule and Spring together, but configuring Mule in Spring bean Xml can be a verbose process. Now you can configure it using Mule Xml and mix Spring beans in the configuration.
1.1.1.
????????
Using Spring as a Component Factory
我下面首先嘗試的是第一種。
1.1.1.1.?????
web.xml
?????? <context-param>
????????????? <param-name>contextConfigLocation</param-name>
??????? <param-value>/WEB-INF/applicationContext-mule.xml
??????? </param-value>
?????? </context-param>
?
?????? <listener>????????????? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
??? </listener>
1.1.1.2.?????
配置文件(舉例)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
?????? <bean id="muleManager" class="org.mule.extras.spring.config.AutowireUMOManagerFactoryBean" destroy-method="dispose" singleton="true"/>
?
??? <bean id="muleNameProcessor" class="org.mule.extras.spring.config.MuleObjectNameProcessor"/>
?
??? <bean id="muleClient" class="org.mule.extras.client.MuleClient" depends-on="muleManager"/>
?
??? <bean id="applicationEventMulticaster" class="org.mule.extras.spring.events.MuleEventMulticaster">
??????? <property name="asynchronous">
??????????? <value>false</value>
??????? </property>
??? </bean>
?
??? <bean name="JmxAgent" class="org.mule.management.agents.JmxAgent"/>
??? <bean name="JdmkAgent" class="org.mule.management.agents.JdmkAgent">
??????? <property name="jmxAdaptorUrl" value="http://localhost:9999"/>
??? </bean>??
?
??? <bean id="echoDescriptor" class="org.mule.impl.MuleDescriptor">
??????? <property name="inboundEndpoint">
??????????? <ref local="echoInboundEndpoint"/>
??????? </property>
??????? <property name="implementation">
??????????? <value>echoComponent</value>
??????? </property>
?? </bean>
??
??? <bean id="echoInboundEndpoint" singleton="false" class="org.mule.impl.endpoint.MuleEndpoint">
??????? <property name="endpointURI">
??????????? <bean class="org.mule.impl.endpoint.MuleEndpointURI">
??????????????? <constructor-arg index="0">
??????????????????? <value>vm://echo</value>
??????????????? </constructor-arg>
??????????? </bean>
??????? </property>
??? </bean>
?
?????? <bean id="echoComponent" class="org.mule.components.simple.EchoComponent" singleton='false'/>
</beans>
|
1.1.1.3.?????
評價
這種方法是比較純的
spring
配置手段
(
注意在此使用的
DTD
為
spring-beans.dtd)
,不利于把握
mule
的配置感覺。不推薦使用。
1.1.2.
????????
Configuring a Spring context using Mule Xml
1.1.2.1.?????
Web.xml
?????? <context-param>
????????????? <param-name>contextConfigLocation</param-name>
??????? <param-value>/WEB-INF/applicationContext-mule.xml,
???????????????????? /WEB-INF/ede-config.xml
??????? </param-value>
?????? </context-param>
?
?????? <listener> ??????????? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
??? </listener>
?
1.1.2.2.?????
配置文件(舉例,
applicationContext-ede-core.xml
)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mule-configuration PUBLIC "-//SymphonySoft //DTD mule-configuration XML V1.0//EN" "http://www.symphonysoft.com/dtds/mule/mule-spring-configuration.dtd">
?
<mule-configuration id="EdeCore" version="2.0">
?????? <!--
描述
-->
?????? <description>Enterprice DataExpress</description>???
?????? <mule-environment-properties synchronous="true" serverUrl="tcp://localhost:9999">
????????????? <queue-profile maxOutstandingMessages="1000" persistent="true"/>
?????? </mule-environment-properties>
?
??? <agents>
??????? <agent name="JmxAgent" className="org.mule.management.agents.JmxAgent"/>
??????? <agent name="JdmkAgent" className="org.mule.management.agents.JdmkAgent"/>
????????????? <properties>
???????????????????? <property name="jmxAdaptorUrl" value="http://localhost:9999"/>
????????????? </properties>
??? </agents>
?
?????? <model name="Nothing"></model>
</mule-configuration>
1.1.2.3.?????
配置文件(舉例,
applicationContext-ede-extend.xml
)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mule-configuration PUBLIC "-//SymphonySoft //DTD mule-configuration XML V1.0//EN" "http://www.symphonysoft.com/dtds/mule/mule-spring-configuration.dtd">
?
<mule-configuration id="EDE" version="1.0">
?????? <!--
?????? <description>Enterprice DataExpress</description>???
?????? <mule-environment-properties synchronous="true" serverUrl="tcp://localhost:9999">
?????? <queue-profile maxOutstandingMessages="1000" persistent="true"/>
?????? </mule-environment-properties>-->
??????
??? <mule-descriptor name="echoUMO" implementation="echoComponent">
??????? <inbound-router>
??????????? <endpoint address="vm://echo"/>
??????? </inbound-router>
??? </mule-descriptor>
?
?????? <bean id="echoComponent" class="org.mule.components.simple.EchoComponent" singleton="false"/>
</mule-configuration>
1.1.2.4.?????
評價
這種方法是比較
Mule-friendly
的配置手段
(
注意在此使用的
DTD
為
mule-spring-configuration.dtd)
,從整體感覺來看,與一般的純
Mule
配置感覺類似。
推薦使用。
1.1.3.
????????
自定義方式
從產品研發來看,自定義模型配置加載方式有著諸多的好處,這里不講。
我們的自定義模型配置加載方式的目標是:
1、?????????????
可以兼容標準的基于
mule
配置文件配置的模型,同時也要兼容根據其他定義方式(如基于數據庫)的可編程式模型加載;
2、?????????????
可以更多的干預系統默認的加載方式。
1.1.3.1.?????
Web.xml
<context-param>
??????? <param-name>com.nci.ede.config</param-name>
??????? <param-value>applicationContext-ede-core.xml,
???????????????????? applicationContext-ede-extend.xml,
???????????????????? applicationContext-sample-echo2.xml
??????? </param-value>
??? </context-param>
?
??? <listener>
??????? <listener-class>com.nci.ede.system.config.EdeBuilderContextListener</listener-class>
??? </listener>
?
|
這里有一個重要的
Listener
,用來在系統啟動的時候自動加載配置信息,其核心代碼如下:
public void contextInitialized(ServletContextEvent event)
??? {
??????? String config = event.getServletContext().getInitParameter(CONFIG_INIT_PARAMETER);
??????? if (config == null) {
??????????? config = getDefaultConfigResource();
??????? }
??????? try {
??????????? createManager(config, event.getServletContext());
??????? } catch (ConfigurationException e) {
??????????? event.getServletContext().log(e.getMessage(), e);
??????? }
??? }
??? protected UMOManager createManager(String configResource, ServletContext context) throws ConfigurationException{
??????? //WebappMuleXmlConfigurationBuilder builder = new WebappMuleXmlConfigurationBuilder(context);
??????? SpringConfigurationBuilder builder = new SpringConfigurationBuilder();
??????? UMOManager manager = builder.configure(configResource);???????
??????? try {
???????
?? //
通過
spring
的
bean factory
獲取
???????
?? EdeConfigurationLoader loader = (EdeConfigurationLoader)SpringHelper.getBean(manager,"edeConfigurationLoader");
???????
?? loader.loadConfig();
?????????????????? } catch (ObjectNotFoundException e1) {
??????????????????????????? e1.printStackTrace();
?????????????????? } catch (EdeException e) {
??????????????????????????? e.printStackTrace();
?????????????????? }
??????? return manager;
??? }
|
其中所調用的自定義加載器
edeConfigurationLoader
可以在
spring
中注入。