从形势来看,如果应用不使用 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 中注入。